package com.hosamazzam.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by hosam azzam on 23/11/2016.
 */

public class CustomFontTextView extends AppCompatTextView {

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

    }

    public CustomFontTextView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
            String fontName = a.getString(R.styleable.CustomFontTextView_fontName);

            try {
                if (fontName != null) {
                    Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
                    setTypeface(myTypeface);
                }
            } catch (Exception e) {
                Log.e("CustomFontTextView", "Unable to load typeface: " + e.getMessage());
            }

            a.recycle();
        }
    }
}
@@ -1418,10 +1418,404 @@
  %7D%0A    %7D
+%0A%0A    public void setFont(String fontName) %7B%0A        try %7B%0A            if (fontName != null) %7B%0A                Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), %22fonts/%22 + fontName);%0A                setTypeface(myTypeface);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(%22Unable to load typeface: %22 + e.getMessage());%0A        %7D%0A    %7D
 %0A%7D

add set font from java