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:
-
version="1.0" encoding="utf-8"?>
android:font="@[package:]font/font_to_include" android:fontStyle=["normal" | "italic"] android:fontWeight="weight_value" /> - elements:
- example:
- XML file saved at
res/font/lobster.xml
:version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"> android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> XML file saved in
res/layout/
that applies the font to aTextView
:version="1.0" encoding="utf-8"?>
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:
-
version="1.0" encoding="utf-8"?>
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
:version="1.0" encoding="utf-8"?>
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"> XML file saved in
res/values/
that defines the cert array:version="1.0" encoding="utf-8"?>
name="certs"> - MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...
XML file saved in
res/layout/
that applies the font to aTextView
:version="1.0" encoding="utf-8"?>
android:fontFamily="@font/lobster" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />