Using Fonts in Android


SPEAKER: Fonts can
play an important role in branding your app, but
using custom fonts used to be quite complicated. On top of this, having the
font files bundled in your app increased your APK
size considerably. With Android O and Support
Library 26 or above, we have added support for
two new features– fonts as resources and
downloadable fonts. This means that you
have another resource folder called Font where you
can put your font assets. These can be TTF, Altura
files, but also XML files where you can define a font
family with different files for styles and weights. You can then reference
fonts from the XML using the fontfamily property in the
Text view or from the code, by getting a reference to the
typeface via the resources. With the Android
Studio integration, you can preview the font
resource or its usage in the Layout Editor. We also now support
downloadable fonts so this means that you don’t
need to bundle your font file in your APK,
but rather the system will download them for you on
demand from a font provider, for example, from Google Fonts
from Google Play services. Moreover, if other apps on
the device use the same font, the system will just reuse it. If the font can’t be downloaded,
the default system one will be used. The font is retrieved
synchronously when the layout
using it is inflated. To avoid the increase of
the first layout time, you can pre-declare fonts that
need retrieving at app start. Define which fonts will be
downloaded in a resource array and add the metadata tag
to declare the resource array in your manifest. You can also use the
font fetch strategy to tell the system to fetch
the font asynchronously with a timeout. So how do you get
to those fonts? In the Layout Editor, select
the text view and then under Properties, select
Font Family, More Fonts. Android Studio allows
you to pick any font from the extensive Google
Fonts catalog, all of them optimized to work on Android. Pick the font you
want and the size whether you want it to be
a downloadable font or not. If you want to avoid
downloading the file, either because you want to have
the font available even when the app is offline or because
you’re targeting devices that don’t use Google
Play services, you can always choose to
bundle the Google font. Font files will be automatically
added to the Font folder and the font
referenced in your XML. To apply the same font
in the entire app, use the Font Family
style item in your theme. Just watch out for any styles
that also use Font Family and can override the app theme. In some cases, you might need
to dynamically download fonts. To implement this, there are two
classes that you need to use– a font request that allows you
to set up which kind of font you want to download
and a fonts contract that allows to create
a typeface object based on the font request. If you’re using the
Support Library, then just use the font request
and fonts compact contract from the support package. The fetching will be
done on a handler thread that you’ll need to
provide, so just make sure that’s not the UI thread. That’s it. In just a few clicks
or a few lines of code, you can improve the
personality of your app by adding fonts, and all of this
with memory, network, and space savings. If you want to find out more
about working with fonts, check out the Android
documentation, the sample code, and the Google I/O talk on
What’s New in Support Library. [MUSIC PLAYING]

Leave a Reply

Your email address will not be published. Required fields are marked *