Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using a Custom Font

In Chapter: Graphics
Author: Ian Darwin ('idarwin')
Published? true
FormatLanguage: WikiFormat


The range of fonts that comes with Android 2.x is amazingly miniscule - three variants of the "Droid" font. You want something better.


Install a TTF or OTF version of your font in assets/fonts (creating this directory if needed). In your code, create a Typeface from the "asset", and call the View's setTypeface() method. You're done!


You can provide one or more fonts with your application. We have not yet seen a published way of installing system-wide fonts. So beware of huge fonts, as they will be downloaded with your application, increasing its size.

Your custom font's format should be TTF or OTF (TrueType or OpenTypeFace, a TTF extension). You need to create the "fonts" subdirectory under "assets" in your project, and install the font there.

While you can refer to the pre-defined fonts just using XML, you cannot refer to your own fonts using XML. This may change someday, but for now the content model of the android:typeface attribute is an XML enumeration containing only "normal", "sans", "serif" and "monospace" - that's it!

So you have to use code. Basic steps are:

  1. Find the View you want to use your font it;
  2. Create a Typeface object from one of the Typeface class' static create() methods;
  3. Message the Typeface into the View's setTypeface method.

There are several Typeface.create() methods, including:

  • create(String familyName, int style);
  • create(TypeFace family, inst style);
  • createFromAsset(AssetManager mgr, String path);
  • createFromFile(File path);
  • createFromFile(String path);

You can pretty well see how most of these should work. The parameter "style" is, as in Java, one of several constants defined on the class representing fonts, here Typeface. Our code example uses the createFromAsset() method so we don't have to worry about font locations. You could probably provide a font shared by several locations using an absolute path into "/sdcard" using the latter two forms; remember to request permission in the AndroidManifest to read the SD Card! You can create representations of the built-in fonts, and variations on them, using the first two forms.

The font I used is the nice Iceberg(tm) font, from SoftMaker Software GmbH. This font is copyright and I do not have permission to redistribute it, so when you download the project and want to run it, you will need to install a TrueType font file at assets/fonts/fontdemo.ttf. Note that if the font is invalid, Android will silently ignore it and use the built-in droid font.

In this demo we provide two text areas, one using the built-in "Serif" font and one using a custom font. They are defined, and various attributes added, in mainx.xml:

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

Here is the source code:

public class FontDemo extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        TextView v = (TextView) findViewById(R.id.FontView);	// 1
        Typeface t = Typeface.createFromAsset(getAssets(),	// 2
        v.setTypeface(t, Typeface.BOLD_ITALIC);			// 3

If all is well, it should look like this:

See Also:

CustomType is an open source library that automates some of this work; see http://haptik.co/tech/2015/01/13/introducing-customtype-an-android-library-for-the-typography-enthusiast/


The source code for this project can be downloaded from http://projects.darwinsys.com/FontDemo-src.zip .


The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/FontDemo.