Skip to content

Most visited

Recently visited

navigation

Working With Fonts

Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources. Android O also provides a mechanism to retrieve information related to system fonts and provide file descriptors.

For more information, refer to the following sections:

Fonts in XML

Android O lets you bundle fonts as resources by adding the font file in the res/font/ folder. 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 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 an XML file that contains multiple font files along with its style and weight details. You can access the font family as a single unit.

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="http://schemas.android.com/apk/res/android">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/lobster_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/lobster_italic" />
    </font-family>
    

Using fonts in XML layouts

You can easily use your fonts, either a single font file or a font from a font family, in a TextView object or in styles. To attach 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 use the fontFamily attribute to access the font file.

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

    You can retrieve fonts by using the getFont(int) method, where you need to pass the resource identifier of the font that you want to retrieve. This method returns a Typeface object. This encodes the first of the weight or style variants of your font, if it is a font family. You can then use the Typeface.create(typeface, style) method to retrieve specific styles.

    Note: The TextView already does this for you.

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

    Retrieving system fonts

    The fonts.xml file describes all the fonts that are preinstalled on the device and their respective details such as family name, weight, and style. Until now, Android didn't expose public APIs to retrieve such system font data. So, some apps accessed it directly to find the system fonts and loaded them on their own instead of using the android font stack. Android O offers new APIs that expose the information in fonts.xml as well as provide FileDescriptor objects to the font files. Apps that directly load fonts are encouraged to move away from that practice and start using the font enumeration API. The new APIs are:

    Accessing APIs from your app

    To access APIs from your app, call the following methods:

    FontManager fontManager = context.getSystemService(FontManager.class);
    FontConfig systemFontsData = fontManager.getSystemFonts();
    

    Note: The FontConfig class includes data on all the defined font families and, for each font within a family, the details about the font's weight and style. It also provides an open file descriptor to the font file. Callers of the API should ensure they close the given file descriptors once they have finished using them.

    This site uses cookies to store your preferences for site-specific language and display options.

    Hooray!

    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 one-minute survey?
    Help us improve Android tools and documentation.