Wednesday, June 15, 2016

Custom Font(s) for TextView in Android (Java)

Using customized fonts in Android OS is not supported by default. You have to essentially put your fonts in an assets folder in TTF or OTF format, then reference it in an extended widget of EditText/TextView or whichever widget you'd want to use.

First copy your ttf to your /assets/ directory in your project root.

The next thing you need to do is create a helper class, mine is called TextViewHelper.java ->

package com.yourdomain.yourapp.helper;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import com.yourdomain.yourapp.R;

public class TextViewHelper extends TextView {
    private static final String TAG = "TextViewHelper";
    private static final String fontname = "SourceSansPro-Semibold.ttf";

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

    public TextViewHelper(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewHelper(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewHelper);
        String customFont = a.getString(R.styleable.TextViewHelper_customFont);
        setCustomFont(ctx);
        a.recycle();
    }
    /* force custom font */
    public boolean setCustomFont(Context ctx) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), fontname);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }

}

Mine forces a custom font programmatically. Though there are many ways to skin this cat, this way I am assuming all fonts need to be the same, and I am using only one. You could theoretically expose and use numerous fonts instead of just one, but this simply does only one, SourceSansPro-Semibold.ttf.

The next and final step is you have to reference it from your layout.xml(s). See below sample:

Wherever you have <TextView /> tags referenced change it to:

            <com.yourdomain.yourapp.helper.TextViewHelper
                android:id="@+id/hm_vpn_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:textColor="@color/test_color"
                android:textSize="16sp"
                android:text="@string/yourstring" />

And voila! You have custom fonts.

No comments:

Post a Comment