AnnotationSpan / Fonts / Rialto / Spans / Text

Rialto: Downloadable Fonts

Rialto is a new Android text styling library based upon Annotation Spans. Rialto enables you to provide consistent text formatting throughout your app by using annotations in your string resources. Previously we have looked at how to use Rialto, some of the features added to V1.1.0, and I am extremely pleased to release Rialto 1.2.0 which contains another (hopefully!) useful new feature.

Downloadable fonts have been around since the Oreo developer previews, and were made backwardly compatible Support Library 26, so there’s really no reason not to be using them. When downloading these on-demand it is really important to add meta-data to the manifest so that these will get downloaded when your app is installed. This ensures that when your app is launched for the first time all of the required fonts have been downloaded to the device. I’m please to announce that there is now a mechanism to easily hook these in to Rialto so that you can use these in annotations.

The key to this is by using the preloaded_fonts array that is used when pre-declaring the fonts in the manifest. This array will actually be generated for you automatically if you use the tools built in to Android Studio to manage downloadable fonts. It is this array that Rialto uses. A typical @arrays/preloaded_fonts file looks something like this:

This defines two fonts which are used by the app – named “audiowide” and “pacifico”. To hook these up to Rialto we use FontRegistrar which was added to Rialto V1.2.0. We can use this either within our Application instance to provide support throughout the app, or in individual Activities to limit availability to specific Activities. Here we are applying it to the Application:

The FontRegistrar takes a single context as an argument. FontRegistrar contains a single method named registerFonts() which takes three arguments: The first is the RialtoRegistry to which the fonts will be added (RialtoDelegate implements RialtoRegistry so can be used here also); the second is the attribute name to which the fonts will be registered; and the third is the ID of the preloaded_fonts array. FontRegistrar will iterate through the array and register each font using the name from the second argument, and a value equal to the font name which will create a Span to use the relevant font wherever it is encountered.

Using the preloaded_fonts array that we looked at earlier would result in two items being registered, one for each font. An example will show this in action and will probably be easier to understand that the dry description:

The attribute names match those of the second argument to the registerFonts() method, and the values correspond to the font names from the preloaded_fonts array. Running this results in:

It is worth noting that since V1.2.0 Rialto is case insensitive, so using a font name of “Pacifico” in your string resource annotation is identical to using “pacifico”.

There’s really nothing more to this – registering your preloaded_fonts array using FontRegistrar does all that you need, and you just need to use the appropriate annotations within your string resources to make use of them.

That’s where we’ll leave Rialto for now. If you have any issues or feature requests please raise an issue.

Rialto is available on JCenter, and examples can be found in the GitHub repo.

© 2018, Mark Allison. All rights reserved.

Copyright © 2018 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.