Monday, April 27, 2015

Add Custom Fonts to Android TextView — assets and res/font examples

In Android, you can set custom fonts on TextView controls to support different languages or to change UI typography. When I added Kannada language support to a Panchanga application (SRSM Mantralaya), this approach worked well: you need a font file (usually a .ttf) and then you load it and apply it to your TextView.

Where to put the font file

Place your font file in the app's assets folder. The original post used an assets/font folder; common conventions are assets/font/ or assets/fonts/. For the examples below I use assets/font/MyFont.ttf.

Note: For modern Android apps consider using res/font/ with Fonts in XML (API 26+) or the AndroidX support libraries. Those approaches allow you to set fonts directly in XML and support downloadable fonts.

Basic steps

  1. Put the font file (MyFont.ttf) in assets/font/.
  2. Get a reference to the TextView in Java.
  3. Create a Typeface from the asset using Typeface.createFromAsset().
  4. Apply it with setTypeface().

Example — layout XML

<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample text in custom font" />

</LinearLayout>

Example — Activity Java code

// in your Activity
TextView text = (TextView) findViewById(R.id.textview1);
Typeface tf = Typeface.createFromAsset(getAssets(), "font/MyFont.ttf");
text.setTypeface(tf);

(From the original: TextView text = (TextView)findViewById(R.id.textview1);)

Alternative: caching Typeface (recommended)

Creating a Typeface from assets is moderately expensive. Cache typefaces to avoid repeated loads (e.g., when many views use the same font):

public class FontCache {
    private static final HashMap<String, Typeface> cache = new HashMap<>();

    public static Typeface getTypeface(Context ctx, String name) {
        Typeface tf = cache.get(name);
        if (tf == null) {
            try {
                tf = Typeface.createFromAsset(ctx.getAssets(), "font/" + name);
            } catch (Exception e) {
                return null;
            }
            cache.put(name, tf);
        }
        return tf;
    }
}

// usage:
text.setTypeface(FontCache.getTypeface(this, "MyFont.ttf"));

The original images

CF1

CF2

CF3

(These are the original images from the post; add alt text if your platform allows.)

Modern alternatives (recommended for new apps)

  • Fonts in XML (res/font/) — supported natively on API 26+. Allows setting fontFamily in XML. Preferable for consistent theming and easier use in styles.
  • AndroidX Core library (or Support Library) backport for Fonts in XML — lets you use fonts on older Android versions.
  • Downloadable Fonts — reduces APK size; useful if using Google Fonts.

Example using res/font (API 26+)

  1. Put MyFont.ttf in res/font/my_font.ttf.
  2. In layout XML:
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/my_font"
    android:text="Sample text" />

This avoids Java code for Typeface on supported devices.

Troubleshooting and tips

  • File path and name are case-sensitive on some platforms — use exact names.
  • If createFromAsset() throws an exception, verify the file path and that the font is a valid TTF/OTF.
  • Cache Typeface objects to avoid UI lag and repeated asset reads.
  • Check font licensing before bundling it in your APK.

Summary

  • Put your .ttf in assets/font/ (or res/font/ for modern approach).
  • Load with Typeface.createFromAsset() and call setTypeface() on your TextView.
  • Prefer res/font / Fonts in XML or AndroidX if you need XML-based fonts and broader platform features.

That's it. Try it. Happy Coding.

No comments: