If you haven't already heard, Android O has officially been named Android Oreo! You can learn more on the Official Android Oreo Homepage.

In Android Oreo, Google has continued their efforts to make Android smarter, faster, and more powerful than before. Text changes incorporate various new features that allow developers to reduce APK sizes, minimize users' down time, and create a more dynamic look and feel for their applications. These new features include fonts that take up less memory, text input fields that can be automatically filled to save time, and text fields that dynamically adjust themselves based on size constraints. This results in an application that is faster to interact with and more attractive to download. I will cover more benefits later in this post.

Sample Application

To gain further insight into fonts, autofill, and resizing text, you can look at this simple sample application. The sample application covers the basics of the features mentioned in the remaining portion of this blog. Due to several new updates in Android Studio that are necessary for these features, this application was created using Android Studio 3.0. You can download the preview version here.

Fonts

Before Android O, developers had to pack all necessary fonts into their APKs. Now, you can use Downloadable Fonts. Downloadable Fonts use a font provider that retrieves fonts and caches them locally on the device so other applications can request and share fonts. This results in a reduction of APK size, which is important because sometimes apps fail to install when the phone is out of memory. Users may also consider the size of the app to be too much and decide not to install it. Downloadable Fonts also benefit the overall system health. Since multiple applications can now use the same font, users save cellular data, phone memory, and disk space.

Downloadable Font process flow

Implementation

There are many ways to take advantage of Downloadable Fonts. One is to use the Layout Editor in Android Studio. Another way is to programmatically request fonts in your code.

Layout Editor

In order to use this option, you need to make sure you are using Android Studio 3.0.

  • Select a TextView in the Design tab of a layout file, navigate to Attributes (Properties in Android Studio before 3.0), go the the fontFamily dropdown, scroll down and select More Fonts...you now have access to over 800 font families listed in Google Fonts.

More Fonts Dropdown Screen

  • In the Resources window, scroll through the fonts, select a font, and click ok.

Resources Screen

Note that there are two options before clicking ok, Create downloadable font and Add font to project. The first one creates a Downloadable Font and the second bundles the font file into your application.

That's it! You have now successfully set up a Downloadable Font.

Programmatically

This is where the real fun begins!

To request a font from the Google Fonts provider, you need to use the following values in a query format.

Font Query Table

In the sample app, width is set to always 1 and besteffort is set to always true. Besteffort means if one of the numeric values does not match up exactly, the provider would return the next best option. To build the query itself, Google provided a QueryBuilder class.

QueryBuilder queryBuilder = new QueryBuilder("Alfa Slab One")
 .withWidth(1)
 .withWeight(430)
 .withItalic(0.86)
 .withBestEffort(true);
String query = queryBuilder.build();

The code above would set query to "name=Alfa Slab One&weight=430&width=1.0&italic=0.86&besteffort=true". You then use query to create a FontRequest.

FontRequest request = new FontRequest(
 "com.google.android.gms.fonts",
 "com.google.android.gms",
 query,
 R.array.com_google_android_gms_fonts_certs);

The first parameter is the font provider authority. The second parameter is the font provider package to verify the identity of the provider. The third is the query that QueryBuilder generated, and the last parameter is a list of hashes for the certificates to verify the identity of the provider. The certificates are only needed if you request the fonts through the support library.

Once the request is created, you need to create an instance of the FontsContractCompat.FontRequestCallback class and override onTypefaceRetrieved() and onTypefaceRequestFailed() to update the application as needed.

FontsContractCompat.FontRequestCallback callback = new FontsContractCompat.FontRequestCallback() {
 @Override
 public void onTypefaceRetrieved(Typeface typeface) {
 //update UI to reflect new font using typeface
 }
 @Override
 public void onTypefaceRequestFailed(int reason) {
 //show error message using the parameter reason
 }
};

Lastly, you call requestFont to put everything together and request the font. You just need to pass in the context, the request created earlier, the callback created earlier, and a handler to fetch fonts on a thread.

FontsContractCompat.requestFont(context, request, callback, handler);

Make sure the handler is not the UI thread handler.

The sample app takes input from the user and then passes them to the QueryBuilder to update the font of the text on the screen once you click GET.

Fonts Activity Example

Pre-declaring Fonts

When a user first uses the application after installation, a request is sent to the provider to fetch the font. This can lead to a delayed layout time since layout inflation and resource retrieval are synchronous tasks. Once the font is retrieved, it is ready to use right away. If a problem arises during the retrieval process and it fails, the default font is used. To avoid the delay, you can pre-declare fonts to be prefetched. To pre-declare a font, create a resources array in res/values/arrays.xml and specify which fonts should be prefetched.

@font/example_one@font/example_two

Once the fonts are declared, go to the manifest and declare the resource array.

Autofill

As mentioned earlier, Google is always looking to make the user experience flow faster. Android O and the Autofill Framework accomplish this by allowing users to save time filling out forms and help minimize user input errors. The Autofill Framework manages communication between the application and an autofill service which allows it to recognize certain views and automatically pull saved values in to populate them. An Autofill Service must be enabled in the system settings. To do this, go to Settings -> System -> Languages & Input -> Advanced -> Input Assistance -> Autofill Service. You can create your own, however, there are 3rd party applications that already take care of it. For the sample application, 1Password was used, however, any password manager that has updated for Android O will work.

Luckily, Android O and the Autofill Framework should work automatically with very little code added. For this blog, the sample application simply has an example showing how Autofill would look and feel with 1Password. In the sample application, to save fields in 1Password you need to first set up a beta for Android O account on the 1Password website. Once your beta account is set up, login to 1Password on an Android O device or emulator and navigate to the Autofill Example page in the sample application. Once you enter values for both fields and click Login, you will be sent back to the landing page and you will be prompted to save those values in 1Password.

When you navigate back to the Autofill Example page and click on a field, an option to use 1Password shows up.

Before Autofill chosen

Once this option is clicked, the fields are populated and you are ready to login.

After Autofill chosen

What's added for developers?

Developers can add hints for fields to help the autofill service correctly classify what type of data should be in a view. To add a hint, either call setAutofillHints() or set the android:autofillHints attribute using any of the predefined values like AUTOFILL_HINT_USERNAME or AUTOFILL_HINT_CREDIT_CARD_NUMBER. A full list of predefined values is listed on the Android reference site for View. The predefined values include hints for:

  • credit card expiration date
  • credit card expiration day
  • credit card expiration month
  • credit card expiration year
  • credit card number
  • credit card security code
  • email address
  • name
  • password
  • phone
  • postal address
  • postal code
  • username

Developers can also mark fields as important for autofill by calling setImportantForAutofill() and passing in the mode. By default, views use IMPORTANT_FOR_AUTOFILL_AUTO which leaves it up to the Android system to decide if the view is important or not. There is also IMPORTANT_FOR_AUTOFILL_YES and IMPORTANT_FOR_AUTOFILL_NO to explicitly mark views as important or unimportant. In cases when entire view structures need to be marked, append EXCLUDE_DESCENDANTS to the yes or no mode in a root view to mark its children as important or unimportant for autofill.

Autosizing TextViews

Android O lets you tell a TextView to change the size of its text dynamically to fill its layout based on the characteristics set. Autosizing TextViews make it easier to format text across the wide variety of different screens running Android. There are 3 ways to set up a TextView to autosize. They can be set up in XML or programmatically. If you set up autosizing in XML, it is not recommended to use wrap_content for width or height due to possible unexpected results. In my testing, I tried both XML and programmatically and they seemed to be identical when autosizing. For the sample application, the TextView is set up through XML.

Default

For the default setting, add autoSizeTextType and set it to none or uniform.

<?xml version="1.0" encoding="utf-8"?>

Uniform makes the TextView scale uniformly both horizontally and vertically.

Granularity

Another way is to set a minimum and maximum text size and then define the size of each step between the two.

<?xml version="1.0" encoding="utf-8"?>

The XML above sets the text size range from 12 to 100 and changes size in increments of 2.

Preset Sizes

The last way is to define an array of preset sizes in res/values/.

10sp12sp20sp40sp100sp

Once the array is defined, call it in the XML of the TextView for which you want to set up autosizing.

<?xml version="1.0" encoding="utf-8"?>

This way, you can choose which sizes you want the TextView to jump through.

You can see how this looks in real time in the sample application. Simply navigate to the Autosizing Textviews Example page and start typing in the text field. Once you start filling up the text area, the text size will shrink down to make room for the incoming words. If you prefer to set up autosizing programmatically, follow the steps described on the Android reference site for Autosizing TextViews.

Autosize Example gif

Conclusion

Fonts, Autofill, and Autosizing TextViews are beneficial for everyone. They help developers slim down their APKs, boost download success rates, and improve the users' experience when interacting with text.

These three features are only a small part of the exciting new features that Android O will bring. I highly recommend visiting Android's website to learn more. You can also find more blogs about new features in Android O in the Insights section of CapTech's website.

Sources

https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html

https://developers.google.com/fonts/docs/android

https://developer.android.com/guide/topics/text/autofill.html

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html