Google I/O 2017 introduced API that permits the consumer to request fonts from the applying, as an alternative of attaching them inside APK. This characteristic is out there on gadgets with Android 4.0 and better by means of Assist Library 26. There are about 850 open supply fonts obtainable that can be utilized for any function (together with industrial). All of them will be seen on Google Fonts web page.
[embedyt]https://www.youtube.com/watch?v=V6-roIeNUY0&width=630&peak=350&begin=580[/embedyt]
There are a number of benefits of utilizing Downloadable Fonts:
- Decreased APK dimension, as a result of there isn’t any extra want to connect fonts to the applying,
- downloaded fonts are shared throughout a number of purposes so it improves the general well being of the system.
Fig. 1 Downloadable Fonts course of
Determine 2 reveals the simplified strategy of downloading fonts. As you possibly can see, each utility will get fonts from FontProvider by means of FontContract. When a font is requested by an utility, FontProvider checks if the specified font is already cached. If not, API will mechanically obtain and cache it. Then, the font is offered to the applying. Sounds somewhat bit sophisticated, however implementation is straightforward.
Let’s get began!
Downloadable fonts API require Assist Library 26 or newer, so let’s begin by including it to module construct.gradle
file:
It’s price including that latest appcompat lib moved from SDK Supervisor to Google’s Maven repository, so it’s additionally required so as to add google()
to repositories contained in the undertaking’s construct.gradle
:
For example I’ll use the next format:
Downloadable fonts by way of XML
To set downloadable font by way of XML go to Structure Editor, choose TextView, after which beneath Properties, choose fontFamily > Extra fonts… as proven in determine 2.

Fig. 2 Deciding on a font for the chosen TextView
Set supply to Google Fonts, choose the specified font, verify “Create downloadable font” and press OK.

Fig. 3 Deciding on a downloadable font
Android Studio will mechanically create three information:
- res/values/font_certs.xml
- res/font/selected_font_name.xml (in my case abril_fatface.xml)
- res/values/preloaded_fonts.xml
res/values/font_certs.xml
When a font supplier is just not preinstalled or if you’re utilizing the help library (our case), certificates have to be declared to confirm the font supplier’s identification.
res/font/abril_fatface.xml
The place:
providerAuthority
— the authority of the Font Supplier for use for the request,providerPackage
— the bundle for the Font Supplier for use for the request. That is used for verifying the identification of the supplier,question
— the question to be despatched over to the supplier,certificates
— a useful resource array with a listing of units of hashes for the certificates the supplier ought to signal with. That is used for verifying the identification of the supplier.
This file supplies metadata to Android Studio, so it may be later known as @font/abril_fatface
or R.font.abril_fatface
.
res/values/preloaded_fonts.xml
This generated file is just not important, but it surely improves consumer expertise. Structure inflation and useful resource retrieval are often synchronous duties (let’s overlook about AsyncLayoutInflater for now). By default, the primary try to retrieve fonts triggers a request to FontProvider
which can enhance the primary format inflation time. To keep away from it, we are able to pre-declare fonts inside AndroidManifest.xml with the next line:
After the system will get the font from FontProvider
it’s instantly avaiable. It’s price mentioning, when font retrieval takes longer time than anticipated, the entire course of is cancelled and the applying will show a default font.
Now we’re able to set our font inside XML
Let’s add the next line to our TextView
:

Fig. 5 Modified font by way of XML
The chosen font has been efficiently modified with one line (Fig. 5), however in most situations, we need to use the identical font in all the app, however including tofontFamily
each view we need to change will obfuscate our code. Fortuitously, downloadable fonts are additionally appropriate with kinds. Let’s take a look at the instance:
We have to guarantee that our AppTheme
model is about to the applying inside AndroidManifest.xml:
The outcome if proven under (Fig. 6):

Fig. 6 Default downloadable font for all the utility
Downloadable fonts by way of Code
Downloadable fonts will also be set dynamically in code with techniqueFontsContractCompat.requestFont(ultimate @NonNull Context context, ultimate @NonNull FontRequest request, ultimate @NonNull FontRequestCallback callback, ultimate @NonNull Handler handler)
.
Let’s declare just a few font names:
The perform that creates the FontRequest
object with a particular font identify:
Word that parameters are the identical as inside res/font/abril_fatface.xml file.
Instance of FontRequestCallback
:
Lastly, let’s set the button onClickListener
to name therequestFont
technique :
Outcomes of labor are proven under:
[embedyt] https://www.youtube.com/watch?v=DRuQkRxXZgs[/embedyt]
All samples from this text can be found as a working undertaking within the repository linked here.
#Downloadable #Fonts #Android #Assist #Library #ITgenerator