Can I embed a custom font in an Android application?

AndroidFonts

Android Problem Overview


I would like to have an app include a custom font for rendering text, load it, and then use it with standard elements like StaticText. Is this possible?

Android Solutions


Solution 1 - Android

Yes you can, you jsut can't define it into xml layouts. You need to use it dynamically each time. Check this tutorial for instance.

In case link is dead, here is a sum up of the stuff :

  • Get a font file like times.otf

  • Drop it in your asset folder, inside a "fonts" folder

  • Get a reference of TextView with something like that:

      TextView tv = (TextView) findViewById(R.id.myCustomTVFont);
    
  • Grab you font from the asset folder:

      Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/times.otf");
    
  • Make your TextView look great:

      tv.setTypeface(tf);
    

Solution 2 - Android

As of Android 8.0 (API level 26), you can use fonts in XML. See the documentation here.

Solution 3 - Android

Your can take look in this thread as well to set custom fonts for all the views in your activity.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPentium10View Question on Stackoverflow
Solution 1 - AndroidSephyView Answer on Stackoverflow
Solution 2 - AndroidMattView Answer on Stackoverflow
Solution 3 - AndroidChaitanya KView Answer on Stackoverflow