Kyle Banks

Applying Custom TTF Fonts in an Android Application Using Typeface

Written by @kylewbanks on May 23, 2013.

Everyone knows using the default font in an application can get a little boring. The new Roboto typeface in Android is all well and good, but it can leave something to be desired at times. Luckily, using a custom font is quite easy.

The first step is to create a new folder under your assets directory called fonts. Once that is done, you can simply put your TTF font file in the directory ($PROJECT/assets/fonts/my_font.ttf).

Initializing and applying the Typeface object is simple enough:

Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/my_font.ttf");
myTextview.setTypeface(typeface);

There is however one problem with this approach. Let's say you have my_font.ttf and my_font_bold.ttf. If your TextView has the bold text style, it will be ignored because you applied the normal (non-bold) font. The same issue applies for italic styles.

A Cleaner Solution

Now that we have to do a check for bold (or italic) text styles, the code is going to be a little longer. Since we don't want to repeat this code throughout each Activity or Fragment that applies our custom font, I prefer to put the code in the Application class.

public class MyApplication extends Application {
	
    private Typeface normalFont;
    private Typeface boldFont;
	
    ...
	
    // -- Fonts -- //
    public void setTypeface(TextView textView) {
        if(textView != null) {
            if(textView.getTypeface() != null && textView.getTypeface().isBold()) {
                textView.setTypeface(getBoldFont());
            } else {
                textView.setTypeface(getNormalFont());
            }
        }
    }

    private Typeface getNormalFont() {
        if(normalFont == null) {
            normalFont = Typeface.createFromAsset(getAssets(),"fonts/my_font.ttf");
        }
        return this.normalFont;
    }

    private Typeface getBoldFont() {
        if(boldFont == null) {
            boldFont = Typeface.createFromAsset(getAssets(),"fonts/my_font_bold.ttf");
        }
        return this.boldFont;
    }
}

The setTypeface method will perform the check for bold text styling (again, you can apply this principle to italics as well), and apply the appropriate Typeface accordingly. In addition, rather than initialize the Typeface objects each time, it will only load it from the file system once, and keep it in memory from then on. This should provide a noticeable performance improvement if you are modifying the font of many TextView objects.

Now from your Activity (or Fragment) you can apply the style like so:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        MyApplication application = (MyApplication) getApplication();
        TextView myTextView = (TextView) findViewById(R.id.my_textview);
        application.setTypeface(myTextView);
    }
}

Use the MyApplication#setTypeface method for each TextView you want to style, and the appropriate font will be applied for you.

Let me know if this post was helpful on Twitter @kylewbanks or down below!