A font resource defines a custom font that you can use in your app. Fonts can be individual font files or a collection of font files, known as a font family and defined in XML.
Also see how to define fonts in XML or instead use Downloadable Fonts.
Bundled font
You can bundle fonts as resources in an app. Fonts are compiled in the
R
file and are automatically available in the system as a
resource. You can then access these fonts with the help of the
font
resource type.
- file location:
res/font/filename.ttf
(.ttf
,.ttc
,.otf
, or.xml
)
The filename is used as the resource ID.- resource reference:
- In XML:
@[package:]font/font_name
- syntax:
-
<?xml version="1.0" encoding="utf-8"?> <font-family> <font android:font="@[package:]font/font_to_include" android:fontStyle=["normal" | "italic"] android:fontWeight="weight_value" /> </font-family>
- elements:
- example:
- XML file saved at
res/font/lobster.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>
XML file saved in
res/layout/
that applies the font to aTextView
:<?xml version="1.0" encoding="utf-8"?> <EditText android:fontFamily="@font/lobster" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />
Downloadable font
A downloadable font resource defines a custom font that you can use in an app. This font isn't available in the app itself. Instead, the font is retrieved from a font provider.
- file location:
res/font/filename.xml
The filename is the resource ID.- resource reference:
- In XML:
@[package:]font/font_name
- syntax:
-
<?xml version="1.0" encoding="utf-8"?> <font-family android:fontProviderAuthority="authority" android:fontProviderPackage="package" android:fontProviderQuery="query" android:fontProviderCerts="@[package:]array/array_resource" />
- elements:
- example:
- XML file saved at
res/font/lobster.xml
:<?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="Lobster" android:fontProviderCerts="@array/certs"> </font-family>
XML file saved in
res/values/
that defines the cert array:<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="certs"> <item>MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...</item> </string-array> </resources>
XML file saved in
res/layout/
that applies the font to aTextView
:<?xml version="1.0" encoding="utf-8"?> <EditText android:fontFamily="@font/lobster" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />