Tuesday, 11 April 2017

How to make Custom Fontable TextView...

Follow the Stepas to make Fontable TextView

1) Create Custom class that extends TextView.  (FontableTextView.java)


public class FontableTextView extends TextView {

    public FontableTextView(Context context) {
        super(context);
    }

    public FontableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        UiUtil.setCustomFont(this, context, attrs,
                R.styleable.com_github_browep_customfonts_view_FontableTextView,
                R.styleable.com_github_browep_customfonts_view_FontableTextView_font);
    }

    public FontableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        UiUtil.setCustomFont(this,context,attrs,
                R.styleable.com_github_browep_customfonts_view_FontableTextView,
                R.styleable.com_github_browep_customfonts_view_FontableTextView_font);
    }
}

2) Create Attributre in res-> values-> attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources>


<declare-styleable name="com.github.browep.customfonts.view.FontableTextView">
    <attr name="font" />
</declare-styleable>

</resources>


3) Add UiUtil.java class

public class UiUtil {

    public static final String TAG = "UiUtil";

    public static void setCustomFont(View textViewOrButton, Context ctx, AttributeSet attrs, int[] attributeSet, int fontId) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, attributeSet);
        String customFont = a.getString(fontId);
        setCustomFont(textViewOrButton, ctx, customFont);
        a.recycle();
    }

    private static boolean setCustomFont(View textViewOrButton, Context ctx, String asset) {
        if (TextUtils.isEmpty(asset))
            return false;
        Typeface tf = null;
        try {
            tf = getFont(ctx, asset);
            if (textViewOrButton instanceof TextView) {
                ((TextView) textViewOrButton).setTypeface(tf);
            } else if (textViewOrButton instanceof EditText) {

                ((EditText) textViewOrButton).setTypeface(tf);

            } else {
                ((Button) textViewOrButton).setTypeface(tf);
            }
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: " + asset, e);
            return false;
        }

        return true;
    }

    private static final Hashtable<String, SoftReference<Typeface>> fontCache = new Hashtable<String, SoftReference<Typeface>>();

    public static Typeface getFont(Context c, String name) {
        synchronized (fontCache) {
            if (fontCache.get(name) != null) {
                SoftReference<Typeface> ref = fontCache.get(name);
                if (ref.get() != null) {
                    return ref.get();
                }
            }

            Typeface typeface = Typeface.createFromAsset(
                    c.getAssets(),
                    "fonts/" + name
            );
            fontCache.put(name, new SoftReference<Typeface>(typeface));

            return typeface;
        }
    }

}



4) Now How to use in XML Layout file

 - Add this line to your XML root element (e.g LiniarLayout, RelativeLayout)

       xmlns:app="http://schemas.android.com/apk/res-auto"



-  copy your font to assests-> fonts Directory (e.g HelveticaNeueLight.ttf is in my fonts folder in assests)

<your.package.name.FontableTextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="This is Fontable TextView"
    app:font="HelveticaNeueLight.ttf" />



By Sohel Shaikh


No comments:

Post a Comment

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...