Android Custom Fonts Working with fonts in Android O


Android O changes the way to work with fonts.

Android O introduces a new feature, called Fonts in XML, which allows you to use fonts as resources. This means, that there is no need to bundle fonts as assets. Fonts are now compiled in an R file and are automatically available in the system as a resource.

In order to add a new font, you have to do the following:

  • Create a new resource directory: res/font.
  • Add your font files into this font folder. For example, by adding myfont.ttf, you will be able to use this font via R.font.myfont.

You can also create your own font family by adding the following XML file into the res/font directory:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="">
        android:font="@font/lobster_regular" />
        android:font="@font/lobster_italic" />

You can use both the font file and the font family file in the same way:

  • In an XML file, by using the android:fontFamily attribute, for example like this:


    Or like this:

    <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/myfont</item>
  • In your code, by using the following lines of code:

    Typeface typeface = getResources().getFont(R.font.myfont);