Florina Muntenescu is an Android development technology promotion engineer

Text display is one of the most important tasks for most applications. To help you create a better text experience, we’ve introduced a number of new features in Android Q that continue to improve performance while meeting developers’ needs. These include:

  • By default, automatic hyphenation is disabled.
  • Allows multiple fonts or font families to create a single Typeface;
  • Allows an application to get a list of fonts installed on the device.
  • Optimized some common text style apis.

Auto hyphenation is disabled by default on Android Q and AppCompat V1.1.0

According to performance test reports, up to 70% of the time spent on hyphenation tasks during text analysis is spent when hyphenation is enabled.

Hyphenation takes up 70% of text analysis time

Set the TextAppearance property in the styles.xml file:

<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:hyphenationFrequency">normal</item>
</style>
Copy the code

Set the TextView properties:

<TextView android:hyphenationFrequency="normal" />
Copy the code

Or, call it directly from code:

textView.hyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NORMAL
Copy the code

For more information on hyphenation, check out our share at the Android Dev 2018 Summit.

Use multiple custom fonts in a Single TextView

What if you need a button with both a custom font (Lato font in the image below) and an icon font (small lock symbol in the image below)?

Buttons that contain both ICONS and Latin fonts

Typeface.CustomFallbackBuilder
Up to 64 font families

The concrete implementation of mixed font (icon + Lato) in the above example is as follows:

button.typeface = Typeface.CustomFallbackBuilder(
    // add the Latin font
    FontFamily.Builder(
        Font.Builder(assets, "lato.ttf").build()
    ).build()
).addCustomFallback(
    // add the icon font
    FontFamily.Builder(
        Font.Builder(assets, "icon_font.ttf").build()
    ).build()
).build()
Copy the code

When creating a font family, do not add fonts belonging to different families to the same font family object, nor do you add fonts of the same style to the same font family. For example, putting Lato, Kosugi, and Material in the same font family, or putting two bold fonts in the same family, will result in invalid configuration.

When rendering text using system fonts, developers need to define the desired generic font family (serif, Sans-serif, or Monospace). Call the setSystemFallback() method to set the appropriate system fallback font:

Typeface.CustomFallbackBuilder( FontFamily.Builder( ... ) .build() ).setSystemFallback("sans-serif")
.build()
Copy the code

Text style API updated

Android Q has updated several text style apis:

Better support for variable fonts

TextAppearance now supports the fontVariationSettings property:

<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:fontVariationSettings">... </item> </style>Copy the code

You can set the fontVariationSettings property directly in TextView or AppCompatTextView in Android Q:

<TextView
    ...
    app:fontVariationSettings="..."
/>
Copy the code

Improved Spans API

TextAppearanceSpan now supports typeface, shadow Settings, fontFeatureSettings and fontVariationSettings.

LineBackgroundSpan and LineHeightSpan now provide standards implementation: LineBackgroundSpan. Standard and LineHeightSpan. Standard.

Get system font

Android supports over 100 languages, each containing different fonts and supporting different character sets. Therefore, it is not easy to understand the correspondence between system fonts and character rendering, and applications that handle text rendering themselves, such as games, text readers, and browsers, rely on this information. Starting with Android Q, developers can call the FontMatcher NDK API to get the system-supported font for a specified string.

The system font that can be used to render the text

FontMatcher

// font = NotoSansCJK-Regular.ttc
// length = 2
auto[font, length] = AFontMatcher_match("Delegation TO the university of Aeronautics");

// font = Roboto-Regular.ttf
// length = 8
auto[font, length] = AFontMatcher_match("A.K.A. indication");

// font = NotoSansCJK-Regular.ttc
// length = 2
auto[font, length] = AFontMatcher_match("の な");
Copy the code

FontMatcher API never returns nullPtr:

  • If no font currently supports the given string, a blank square (󟿽) is returned, which is a character deletion.
  • If there is no exact supported style, the font with the closest and most similar style is returned.

To get all available system fonts, call the latest Font Enumeration API. Java developers use SystemFonts getAvailableFonts, please use the NDK developers ASystemFontIterator. Font enumeration results can only change after a system upgrade, so you should cache the results for reuse.

Font update

New Myanmar font

Myanmar font added to Android Q. Myanmar is Unicode compliant and provides native rendering support for Burmese fonts (both Unicode and non-Unicode versions, namely Zawgyi fonts). This means that starting with Android Q, users will be able to switch to Unicode more easily: they will be able to read Both Unicode and non-Unicode text using just one Unicode font — something that was previously impossible. In addition, we have included several new rules in the Android Compatibility Definition Document (CDD) that strictly require ecosphere partners to use Unicode fonts, This includes mandatory-requiring Oems to use a new subtag – “Qaag” – to identify non-Unicode Burmese loci. In the long run, these changes will greatly simplify the lives of developers and reduce fragmentation of the Android ecosystem, leading to a better experience for the 50 million Burmese language users.

The new look

New emojis added to Android Q

Text plays an important role in most applications, so we will continue to invest in improving API features and performance. Check out the video from I/O below to learn about the new apis introduced by Android Q, learn best practices in text development, and let’s work together to make the user experience even better!

To learn more about these Best Practices, watch the full video Best Practices for Using Text in Android (Google I/O’19).

www.youtube.com/watch?v=fpS…

Click here to submit product feedback suggestions