Downloadable and Preloaded Fonts on Android
An important part of making your app look and feel unique is using a great font, or even if you like Roboto you might want to make sure your users see Roboto and not whatever other default the manufacturer has picked. Google made this a lot easier by allowing developers to share fonts from the amazing Google Fonts repository using a technique called Downloadable Fonts. Downloadable Fonts were later made even more efficient when Google introduced Preloaded Fonts which allowed apps to download their fonts at install time instead of at run time. Please note that there are two ways to add downloadable fonts depending on if you intend to use the font in XML or Compose, but there is only one step to make that font preloaded.
Adding a Downloadable Font #
The easiest way to add a preloaded font to an application, is to use the "Resource Manager" tool. To start open Resource Manager from the toolbar by selecting View -> Tool Windows -> Resource Manager. In Resource Manager, select the "Font" tab, if it isn't visible select it in the vertical 3 dots menu. Then click the plus icon and select "More Fonts...". This will bring up a window where you can select fonts from Google Fonts. To see the full list of fonts and filter to find specific fonts, use the Google Fonts website. On the left below the "Font Name", select "Create downloadable font" and then press "OK" to add the font to the project. Three files will be added to the project, a file in the "font" directory that is the name that was put for the "Font Name" this tells Android how to look up the font, "font_certs.xml" that provides certificates for downloading the font, and "preloaded_fonts.xml" which will be covered in the next section.
Make a Downloadable Font Preloaded #
The "preloaded_fonts.xml" file in the "values" directory is what makes the magic work for preloading a font. This file defines the list of font resources that Android will attempt to preload when the app is installed from Google Play. Additionally, the system will optimize this process by only downloading fonts that are not already on the system. This means the app will have a smaller footprint if the font is already on the system as opposed to bundling the font with the application. This results in an overall better user experience as there is no broken font rendering on first load while the font loads and a smaller app size on average for the end user.
Adding a Downloadable Font for Jetpack Compose #
In Jetpack Compose there is an additional way to request a downloadable font using the GoogleFont
class from the androidx.compose.ui.text
package. This allows for a more ergonomic font loading flow in Jetpack Compose. First, add the androidx.compose.ui:ui-text-google-fonts
package to the "build.gradle" file to add the Google Font classes. Then, create a Google font provider to establish the authority, package, and point to the certificates. The "font_certs.xml" file can be added using the previously mentioned method of adding a font using the Resource Manager or can be found linked from the Android documentation. Next define the font using the GoogleFont
class. Finally, define the FontFamily
using the Font
constructor taking a googleFont
and fontProvider
passing the values created in the previous steps. Here is how that all looks in code.
val provider = GoogleFont.Provider(
providerAuthority = "com.google.android.gms.fonts",
proiderPackage = "com.google.android.gms",
certificates = R.array.com_google_android_gms_fonts_certs
)
val roboto = GoogleFont("Roboto")
val robotoFontFamily = FontFamily(
Font(
googleFont = roboto,
fontProvider = provider,
),
)
val robotoTextStyle = TextStyle(
fontFamily = robotoFontFamily,
)
Note that this method does not allow the font to be preloaded as the work is all done in code, so nothing is available at install time. If the app should preload the font, then the method of adding the font using the Resource Manager will be required or the font xml and "preloaded_fonts.xml" files will need to be added manually.
Conclusion #
Downloadable and preloaded fonts provide a superior experience to bundled fonts while ensuring the user has a consistent experience. Users will love the space savings and product owners will love the consistent branding. Unless the font needed is not available through Google Fonts, and will need bundled regardless, using downloadable and preloaded fonts is a win-win situation. Until next time, thanks!
Did you find this content helpful?
Please share this post and be sure to subscribe to the RSS feed to be notified of all future articles!
Want to go above and beyond? Help me out by sending me $1 on Ko-fi. It goes a long way in helping run this site and keeping it advertisement free. Thank you in advance!
- Previous: Avoid "Useless" Cases in Layered Architecture
- Next: Kotlin Exhaustive if