Downloadable Fonts in Android: Developer’s Guide

Fırat Karababa
Material Design in Action
5 min readDec 22, 2017

--

As Android Oreo (O) is announced this year, many new features are introduced to the community including the new font support system. A remarkable feature in the new font support system is Downloadable Fonts. With Android API level 26, instead of including font files into APK file, fonts can be downloaded from a host via network on demand or it can be fetched locally if the font is already downloaded by another app.

Downloadable Fonts provide with certain benefits such as reducing APK size and system memory usage. By using downloadable fonts the fonts to be used are downloaded when required instead of default inclusion in APK and lightweight mobile specific font files are guaranteed to be downloaded instead of generic (mostly not mobile specific) font files. Downloadable Fonts feature also causes less system memory usage and less network traffic since it shares the downloaded font with other applications through a Font Provider service. Image below shows how Downloadable Fonts feature works.

Taken from https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html

As it can be seen from the diagram, all apps requesting same font can get the font through a FontRequest on FontsContract and if the font is already downloaded by another application, there will be no need to download it again; the font will be shared to the requesting app as it was already cached locally.

How to Use Downloadable Fonts?

These are the options to use Android’s downloadable font support:

1- Using downloadable fonts via XML

2- Using Downloadable Fonts programmatically

Lets discuss the advantages/disadvantages of both methods and how to use them.

Some fonts presented in Google Fonts

1- Using downloadable fonts via XML

Declaring the downloadable font for a text in XML is one way to use Downloadable Fonts. By this way the system will download the font for the application asynchronusly.

To use that method, you can follow the following steps:

In the layout editor, select More Fonts.. from fontFamily dropdownlist while the texview is selected.

Choosing More Fonts menu from Properties window

From the following Resources screen choose the font to be used. Here you will be given two options to be able to use that font: Creating downloadable font and adding font to project. If you choose Add Font to Project option, the font will be downloaded and included in your project under res/fonts folder and your app will already include the font file when app is installed in a device. If you continue with Create Downloadable Font option, Android Studio will generate some resource files and XML codes to download this font in your app at runtime. At the top-right corner of the screen, the source option to download the font from is given which currently has only the Google Fonts option.

Choosing font and usage options

Following the previous steps, some files and XML codes are automatically generated for you by Android Studio which are given next.

font-family specifications
Resources array in res/values/arrays.xml
Manifest file declaration

This code snippets/XML files are generated by Android Studio and you do not need to make any additions or changes. The downloadable font feature is ready and the view will be displayed with this font as specified.

The advantage of using Downloadable Fonts in XML is its simplicity. By using downloadable fonts in XML, you let the system do the most of the work including generating necessary files and when and how the font will be downloaded at runtime. The disadvantage of it, on the other hand, is that it can be only used for static font specifications.

2- Using Downloadable Fonts programmatically

The other option to use downloadable fonts is using it programmatically. You can create FontRequest instances when necessary in your code and format your text with the downloaded font. The following code shows how to use downloadable fonts dynamically.

Using Downloadable Fonts programmatically

Three main steps to be followed to use downloadable fonts dynamically are:

  1. Create a FontRequest instance holding the query String which specifies the font features such as font name, font width, font being italic or not. The information on how to form your query String and various font types that can be included in the query can be found in https://developers.google.com/fonts/docs/android and https://fonts.google.com/ . Images below show how to form a query for a FontRequest instance.
Google Fonts query format
Sample queries

2. Create a FontRequestCallback instance to hold the Font download events and make the appropriate callbacks. Fill inside the callback events as you wish.

3. Specify the thread in which font download will occur. In the sample code it is returned by getHandlerThreadHandler() method.

The benefit of the second way of using Downloadable Fonts is the flexibility to change the font with any font at runtime. The main disadvantages of it, however, is the more code you write and having to handle the download process.

Below is the screenshot of the sample app developed to demonstrate the use of downloadable fonts in 2 different ways explained in this guide.

Sample app screenshot

Conclusion

Downloadable fonts which is introduced by Android O has helped us to cope with custom font usages which may sometimes be very painful. This guide aims to be a reference on how to use downloadable fonts in your Android projects. The enhanced version of full Android project used in this article can be found on github. The simpler version of the sample app is also available as initial commit.

Feel free to ask any questions and make comments.

References

Android Developers Page

Google Developers Page

--

--