Skip to content

Most visited

Recently visited


Fonts in XML

Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio. You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.

To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26. For more information on using the support library, refer to the Using the support library section.

To add fonts as resources, perform the following steps in the Android Studio:

  1. Right-click the res folder and go to New > Android resource directory.

    The New Resource Directory window appears.

  2. In the Resource type list, select font, and then click OK.

    Note: The name of the resource directory must be font.

    Adding the font resource directory

    Figure 1. Adding the font resource directory

  3. Add your font files in the font folder.

    The folder structure below generates R.font.dancing_script, R.font.lobster, and R.font.typo_graphica.

    Adding the font files in the resource directory

    Figure 2. Adding the font files in the resource directory

  4. Double-click a font file to preview the file's fonts in the editor. Previewing the font file

    Figure 3. Previewing the font file

Creating a font family

A font family is a set of font files along with its style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, the system can select the correct font based on the text style you are trying to use.

To create a font family, perform the following steps in the Android Studio:

  1. Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
  2. Enter the file name, and then click OK. The new font resource XML opens in the editor.
  3. Enclose each font file, style, and weight attribute in the <font> element. The following XML illustrates adding font-related attributes in the font resource XML:
    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="">
            android:font="@font/lobster_regular" />
            android:font="@font/lobster_italic" />

Using fonts in XML layouts

Use your fonts, either a single font file or a font from a font family, in a TextView object or in styles. To add fonts to the TextView or in styles, use the fontFamily attribute.

Note: When you use a font family, the TextView switches on its own, as needed, to use the font files from that family.

Adding fonts to a TextView

To set a font for the TextView, do one of the following:

The Android Studio layout preview, shown in the rightmost pane of Figure 5, allows you to preview the font set in the TextView.

Previewing fonts in layout preview

Figure 5. Previewing fonts in layout preview

Adding fonts to style

Open the styles.xml, and set the fontFamily attribute to the font file you want to access.

  • <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/lobster</item>
  • Using fonts programmatically

    To retrieve fonts programmatically, call the getFont(int) method and provide the resource identifier of the font you want to retrieve. This method returns a Typeface object. Although the system picks the best style for you from the fonts information, you can use the setTypeface(, int) method to set the typeface with specific styles.

    Note: The TextView already does this for you.

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

    Using the support library

    The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.

    When you declare font families in XML layout through the support library, use the app namespace.

    Note: As of Android Support Library 26.0, you must declare both sets of attributes to ensure your fonts load on devices running Android 8.0 (API level 26) or lower.

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android=""
        <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"
              app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
        <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/myfont-Italic"
              app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />

    To retrieve fonts programmatically, call the ResourceCompat.getFont(Context, int) method and provide an instance of Context and the resource identifier.

    Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
    This site uses cookies to store your preferences for site-specific language and display options.

    Get the latest Android developer news and tips that will help you find success on Google Play.

    * Required Fields


    Follow Google Developers on WeChat

    Browse this site in ?

    You requested a page in , but your language preference for this site is .

    Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

    This class requires API level or higher

    This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

    For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

    Take a short survey?
    Help us improve the Android developer experience. (April 2018 — Developer Survey)