Search code examples
androidfontstextviewandroid-assetsandroid-sdk-2.3

How use Android supplied (custom) fonts?


According to the Android Typography page Roboto font was introduced in Ice Cream Sandwich. I can download the .ttf files there or I can find it (and many others) in the <android-sdk>/platforms/android-x/data/fonts directory (where x is ICS version and higher).

If I want to actually use this font in my app do I still need to copy this to my assets/fonts directory and setup the font like this or is there some other way of accessing it in my layout XML files?

EDIT:

To clarify the question I really mean any of the Android supplied fonts. So if you look in Android Lollipop's font folder (android-sdk/platforms/android-21/data) there are lots of new fonts.

Assume on a single layout I want to use Roboto-Italic in one TextView and (say) NotoSerif-Bold in another. Can I specify that in my XML layout file using android:typeface="..." or do I need to manually copy the required .ttf files to my font folder and subclass the TextView widget?


Solution

  • Hi you would need to set the typeface on a TextView like below:

    Typeface yourTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName + ".ttf");
    yourTextView.setTypeface(yourTypeface);