Try the Compose way
Jetpack Compose is the recommended UI toolkit for Android. Learn how to use text in Compose.

The Downloadable Fonts feature lets APIs request fonts from a provider application instead of bundling files into the app or letting the app download fonts. Downloadable Fonts is available on devices running Android API versions 14 and higher through the AndroidX Core library.
Downloadable Fonts offers the following benefits:
A font provider is an application that retrieves fonts and caches them locally so other apps can request and share fonts. The following figure illustrates the process.
You can use the Downloadable Fonts feature in the following ways, which are discussed in detail' in later sections:
You can set your application to download fonts by using Android Studio 3.0 or higher. To help you get started with Downloadable Fonts features, you can use the font provider from Google Play services.
TextView
. Then, under Attributes,
select fontFamily > More Fonts.
Android Studio automatically generates the relevant XML files that are needed to render the font correctly in your app.
As of Android 8.0 (API level 26), AndroidX Core provides full support for Downloadable Fonts. For more information about using the AndroidX Core library, see the Downloadable Fonts AndroidX Core library section on this page.
To use the Downloadable Fonts feature programmatically, interact with two key classes:
android.graphics.fonts.FontRequest
:
this class lets you create a font request.FontsContractCompat
:
this class lets you create a new
Typeface
object based on
the font request.Your app retrieves fonts from the font provider by using the FontsContract
API. Each
provider has its own set of restrictions on the Android versions and query language it supports. For
more information on the Android versions and query format, refer to your provider's
documentation.
To download a font, complete the following steps:
android.graphics.fonts.FontRequest
class to request the
font from the provider. To create a request, pass the following parameters:
val request = FontRequest( "com.example.fontprovider.authority", "com.example.fontprovider", "my font", certs )
FontRequest request = new FontRequest("com.example.fontprovider", "com.example.fontprovider", "my font", certs);
FontsContract.FontRequestCallback
class.onTypefaceRetrieved()
method to indicate the font request is complete. Provide the retrieved font as the parameter.
You can use this method to set the font as needed. For example, you can set the font on a
TextView
.onTypefaceRequestFailed()
method to receive information about errors in the font request process. For more information
about error codes, refer to the
error code constants.FontsContract.requestFont()
method to retrieve the font from the font
provider. The method initiates a check to determine whether the font exists in the cache. If
the font isn't available locally, it calls the font provider, retrieves the font
asynchronously, and passes the result to the callback. Pass the following parameters:
Context
classandroid.graphics.fonts.FontRequest
classThe following sample code illustrates the overall Downloadable Fonts process:
val request = FontRequest( "com.example.fontprovider.authority", "com.example.fontprovider", "my font", certs ) val callback = object : FontsContract.FontRequestCallback() { override fun onTypefaceRetrieved(typeface: Typeface) { // Your code to use the font goes here. ... } override fun onTypefaceRequestFailed(reason: Int) { // Your code to deal with the failure goes here. ... } } FontsContract.requestFonts(context, request, handler, null, callback)
FontRequest request = new FontRequest("com.example.fontprovider.authority", "com.example.fontprovider", "my font", certs); FontsContract.FontRequestCallback callback = new FontsContract.FontRequestCallback() { @Override public void onTypefaceRetrieved(Typeface typeface) { // Your code to use the font goes here. ... } @Override public void onTypefaceRequestFailed(int reason) { // Your code to deal with the failure goes here. ... } }; FontsContract.requestFonts(context, request, handler, null, callback);
For more information about how to download a font from a font provider, see the DownloadableFonts sample app.
The AndroidX Core provides support for the Downloadable Fonts feature on devices running Android
API versions 14 or higher. The
androidx.core.provider
package contains FontsContractCompat
and FontRequest
classes to implement
the backward-compatible Downloadable Fonts feature support. The AndroidX classes contain methods
similar to the framework methods, and the process for downloading fonts is similar to the one
described in the section on this page about
using Downloadable Fonts programmatically.
To download fonts using AndroidX, import the FontsContractCompat
and
FontRequest
classes from the androidx.core.provider
package. Create
instances of these classes instead of
FontsContract
and
android.graphics.fonts.FontRequest
framework classes.
To use the FontsContractCompat
and FontRequest
classes, you must modify
your app project's classpath dependencies within your development environment.
To add AndroidX Core to your application project, add the following dependency to your app's
build.gradle
file:
dependencies { ... implementation "androidx.core:core-ktx:1.9.0" }
dependencies { ... implementation("androidx.core:core-ktx:1.9.0") }
Android 8.0 (API level 26) and AndroidX Core offer a faster and more convenient way to declare a custom font as a resource in the XML layout. This means that there is no need to bundle the font as an asset. You can define a custom font for your entire theme, which accelerates usability for multiple weights and styles, such as bold, medium, or light, when provided.
res/font
folder.<font-family>
root element and set the font-related attributes, as
shown in the following sample XML file:<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android" android:fontProviderAuthority="com.example.fontprovider.authority" android:fontProviderPackage="com.example.fontprovider" android:fontProviderQuery="example font" android:fontProviderCerts="@array/certs"> </font-family>
@font/font_file_name
in the layout XML file. You can also
use the
getFont()
method to retrieve the file programmatically, such as
getFont(R.font.font_file_name)
.Layout inflation and resource retrieval are synchronous tasks. By default, the first attempt to retrieve fonts triggers a request to the font provider, and therefore increases the first layout time. To avoid the delay, you can pre-declare fonts that need to be retrieved in your manifest. After the system retrieves the font from the provider, it is available immediately. If font retrieval takes longer than expected, the system aborts the fetching process and uses the default font.
To pre-declare fonts in the manifest, complete the following steps:
res/values/arrays.xml
and declare the fonts that you
want to prefetch.res/values/arrays.xml <?xml version="1.0" encoding="utf-8"?> <resources> <array name="preloaded_fonts"> <item>@font/font1</item> <item>@font/font2</item> </array> </resources>
meta-data
tag to declare the resource array in your manifest.<meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts" />
When a font provider isn't preinstalled, or if you are using the AndroidX Core library, declare the certificates the font provider is signed with. The system uses the certificates to verify the font provider's identity.
Perform the following steps to add certificates:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="certs"> <item>MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...</item> </string-array> </resources>
fontProviderCerts
attribute to the array.android:fontProviderCerts="@array/certs"
Starting in Compose 1.2-alpha07, you can use the Downloadable Fonts API in your Compose app to download Google Fonts asynchronously and use them in your app. For more information, see the Compose Downloadable Fonts documentation.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-05-30 UTC.