Android Tutorial: How to use a custom font

Android comes with a nice default font but in some cases you might want to use a different font. In this post I will show you how you can use a custom font in your Android apps.

Include font as resource

The custom font needs to be included in the app as a resource. This resource should be in the assets folder of your project, to keep the project clean it would be useful to create a subfolder for fonts. Create the following folder in your project “assets/fonts”.
Now that the folder is created the font can be added to the project. Copy the custom font to the “assets/font” folder.

Note that the assets folder is not copied from a library project, if you have any libraries that get’s resources from the assets folder you must include these assets in the final published project. http://developer.android.com/tools/projects/index.html

Set typeface on textview

Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf")
TextView textView = (TextView) findViewById(R.id.my_textview);
textView.setTypeface(customFont);

Using the custom font with an adapter

You might wonder how to use your custom font with a list view. The answer lies in the usage of a custom adapter.
I Will use the SimpleCursorAdapter for this example, you can use this technique with most other adapters.

First we need to define the new custom class:

public class CustomFontExampleAdapter extends SimpleCursorAdapter 
{
	public CustomFontExampleAdapter(final Context context, final int layout, final Cursor c, final String[] from,
                final int[] to, final int flags) {
        	super(context, layout, c, from, to, flags);
        }
}

Now we add a bindview function to our class. This function is made to give you control over the item within a list. You can also use this function to make more complex field bindings when working with data, for example you could change the color of a text view or hide an image. You can even use this function to inflate different layouts depending on the data. But that would be a whole article on it’s own, for now we will focus on altering the view as inflated by the adapter itself. We will change the font for a textview in the inflated listview item to our custom (Roboto-Bold) font.

public class CustomFontExampleAdapter extends SimpleCursorAdapter 
{
	public CustomFontExampleAdapter(final Context context, final int layout, final Cursor c, final String[] from,
                final int[] to, final int flags) {
        	super(context, layout, c, from, to, flags);
	}

	@Override
	public void bindView(final View view, final Context context, final Cursor cursor) {
		super.bindView(view, context, cursor);
		final TextView _TextViewTitle = (TextView) view.findViewById(R.id.my_textview);
		
		Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf")
		textView.setTypeface(customFont);							
	}
}

The previous example will work but it will also be a bit inefficient because the bindView is triggered every time a listview item is being set up and we are loading the custom font in the same function. If we move the loading of the font to the constructor of the custom adapter it will be more efficient.

public class CustomFontExampleAdapter extends SimpleCursorAdapter 
{
	private Typeface mCustomFont;

	public CustomFontExampleAdapter(final Context context, final int layout, final Cursor c, final String[] from,
                final int[] to, final int flags) {
        	super(context, layout, c, from, to, flags);
		mCustomFont = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf")
	}

	@Override
	public void bindView(final View view, final Context context, final Cursor cursor) {
		super.bindView(view, context, cursor);

		final TextView _TextViewTitle = (TextView) view.findViewById(R.id.my_textview);		
		textView.setTypeface(mCustomFont);							
	}
}

Creating a helper class

If you are using one font a lot in your project(s) it might be useful to create a helper class to quickly apply your font to a view. The added benefit of this class is that it will give better performance because we can cache the typeface and only load it one.

We start by creating a helper class for our custom font, and create an apply font function

public class RobotoBoldFontHelper
{
	public static TextView applyFont(Context context, TextView textView) {
		return textView;
	}
}

Next we will add a getter for our typeface (singleton), if the typeface has not been loaded yet it will do so otherwise it will return the cached instance.
The added benefit from this function is that the typeface will only be loaded on demand, if not applied it will not load the typeface.

public class RobotoBoldFontHelper
{
	private static Typeface mRobotoBoldFont = null;

	private static Typeface getRobotoBoldTypeface(Context context) {
		if (mRobotoBoldFont == null)
			mRobotoBoldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
		return mRobotoBoldFont;
	}

	public static TextView applyFont(Context context, TextView textView) {
		return textView;
	}
}

The last step is to apply the font to the textview.

public class RobotoBoldFontHelper
{
	private static Typeface mRobotoBoldFont = null;

	private static Typeface getRobotoBoldTypeface(Context context) {
		if (mRobotoBoldFont == null)
			mRobotoBoldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
		return mRobotoBoldFont;
	}

	public static TextView applyFont(Context context, TextView textView) {
		if (textView != null)
			textView.setTypeface(getRobotoBoldTypeface(context));
		return textView;
	}
}

Here is a small example on how to use the helper class.

TextView textView = (TextView) findViewById(R.id.my_textview);
RobotoBoldFontHelper.applyFont(textView);

More information

For more information see: http://developer.android.com/reference/android/graphics/Typeface.html

That was it for this post, I hope it is useful to you.
Till next time.

Leave a Reply

Your email address will not be published. Required fields are marked *