One thing that always bothered me about Android‘s TextView widget is that you couldn’t specify a custom font in the XML layout. It’s not a lot of work to load and apply a font in code, but after finding myself pounding out the same few lines of code on every project, I thought there had to be a better way.

As it turns out, extending TextView and adding a font name property is a breeze. Now I can copy a font into my project, set the name property in my layout, and let the custom class do its thing. In this tutorial, I teach you how to do the same. You can follow along with the step-by-step instructions or download and import the entire project directly into Eclipse.

1. Create a new Android project in Eclipse. Target API 14 (ICS) or better.

2. In the /assets directory (not the /resource directory), create a folder called /fonts. Copy your custom font here. You can use both TTF and OTF fonts. In my case, I included a font called pipe_dream.ttf.

3. In the /res/values folder, create a new file called attrs.xml. This is how the Android SDK lets you name custom properties for your widgets.


4. In /res/layouts, we will need to include our to-be-created custom text view in the activity_main.xml file.


5. In the /src folder, we will want to create our MyTextView class. It extends the standard text view, plucks the font name from the custom attribute, and applies the type face.

package com.authorwjf.customfontdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);


public MyTextView(Context context) {

private void init(AttributeSet attrs) {
if (attrs!=null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
String fontName = a.getString(R.styleable.MyTextView_fontName);
if (fontName!=null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);


6. Because the text view is now self-contained, we aren’t required to make any modifications to our /src/ file.

package com.authorwjf.customfontdemo;

import android.os.Bundle;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {


The application is ready to deploy (Figure A).

Figure A

Despite the simplicity of working with custom fonts, keep in mind the following points, which are based on my observations.

  • Your fonts must live in the asset folder, not the resource folder.
  • I have read documentation claiming OTF fonts are not supported; however, I have had no issues with them.
  • The framework uses the file name of the font, not the font’s internal name property.
  • The font extension (ttf or otf) needs to be lower case.
  • I have run across fonts that simply don’t seem to work despite installing and working fine on my Mac and PC. So if you feel like you are doing everything right and you still aren’t seeing your custom font, try another font file just to be sure it’s your code and not the framework/font that is the problem.