The Autofill Framework, available in Android 8.0 (API level 26) and later, helps users to fill out forms, such as account and credit card forms, by automatically filling the form fields with user data that was previously saved. Most autofill services use heuristics that allow them to fill out screens in client apps without optimizations. However, you can optimize your app to make sure autofill services can fill out its views correctly.

This codelab shows how to set up an autofill service in an emulator, implement several optimizations in your app, and test that your app works as expected with the autofill service.

How does my app work within the Autofill Framework?

The Autofill Framework includes the following:

When a user interaction triggers an autofill request, the Android system creates a hierarchical representation, known as the view structure, of the screen in the client app. The view structure contains information such as the autofill type that can be used in each node and the current value of the node. For more information, see ViewStructure. The Android system then adds the view structure to the autofill request and sends it to the autofill service, who tries to fulfill the request.

The Autofill Framework defines a similar workflow to save user data. For more details about the autofill workflows, see Basic usage.

If the view structure accurately describes the screen in the client app, the autofill service can better fulfill the requests. Furthermore, you can provide additional information specifically created to help autofill services determine how to fulfill the request.

This codelab walks you through the process of providing information that optimize how your app works within the Autofill Framework. The codelab also shows how to configure a device to test the optimizations with the Android Autofill Framework sample.

What you will build

In this codelab, you're going to perform the following tasks:

  1. Configure a test autofill service in your device.
  2. Use the test autofill service to save data that you can use in your tests.
  3. Build a simple app for Android that includes an activity with the following fields:
  • Email
  • Password
  • Captcha
  1. Provide the following optimizations for the simple app:
  1. Test the optimizations with the autofill service.

What you'll learn

What you'll need

Download the Code

Click the following link to download the code for the Android Autofill Framework sample.

Download source code

You can also clone the repo directly from github using the following command:

$ git clone https://github.com/googlesamples/android-AutofillFramework.git

Unpack the downloaded zip file. This will unpack a root folder named android-AutofillFramework, which contains the autofill service to use for testing the optimizations.

Install the autofill service

You can use Android Studio to build and deploy the Android Autofill Framework sample to your device by following these steps:

  1. Open Android Studio and click Import project (Gradle, Eclipse ADT, etc.)
  2. In the Select Eclipse or Gradle Project to Import dialog, choose the folder where you downloaded the autofill service sample. Click OK.
  3. Select afservice from the Run/Debug configuration dropdown and click the Run button.
  4. Select your device from the Select Deployment Target dialog.

Configure the autofill sample as the current autofill service

Only one app can provide autofill services at a time. You must select the autofill sample as the currently configured service. To select the current autofill service:

  1. Open the Settings app in your device.
  2. Open the System > Languages & Input > Advanced > Autofill service option and choose the Multi-Dataset Autofill Service.
  3. Click OK in the Make sure you trust this app dialog.

Save test autofill data

The Android Autofill Framework sample allows the creation of fake data for testing purposes. To create test data:

  1. Open Autofill Settings from the app launcher.
  2. Tap Add fake Autofill data, and select the number of datasets that you would like to use for testing.

Create the client app

  1. In Android Studio, click File > New > New Project...
  2. In the Create Android Project dialog click Next.
  3. In the Target Android Devices dialog, check the Phone and Tablet checkbox and select API 26 or later from the target API dropdown. Click Next.
  4. In the Add an Activity to Module dialog, choose Login Activity. Click Next.
  5. In the Configure Activity dialog, click Finish.

Test the client app without optimizations

  1. In Android Studio, open the client app project.
  2. Click the Run button.
  3. Tap the Email field. The Autofill Framework doesn't offer any suggestions to fill out the form.

Provide autofill hints attributes to client views

  1. In Android Studio, open the client app project.
  1. Open the login_activity.xml file and add the android:autofillHints attribute with a value of username to the Email field, as shown in the following example:
<AutoCompleteTextView
   android:id="@+id/email"
   ...
   android:autofillHints="username"
   ...
   />
  1. Add the android:autofillHints attribute with a value of password to the Password field, as shown in the following example:
<EditText
   android:id="@+id/password"
   ...
   android:autofillHints="password"
   ...
   />

Test the client app with autofill hints optimizations

  1. In Android Studio, open the client app project.
  2. Click the Run button.
  3. Tap the Email field. Note that the Autofill Framework offers suggestions to fill out the screen, as shown in the following screenshot:
  4. Choose one of the datasets. The Autofill Framework fills out the Email and Password fields.

Provide the importantForAutofill attribute to a client view

This exercise shows the behavior of the android:importantForAutofill attribute applied to individual views or to the activity. The importantForAutofill attribute allows you to control which fields are sent to the service for autofill purposes, which can improve the performance of autofill operations.

  1. In Android Studio, open the client app project.
  2. Open the login_activity.xml file and add the android:importantForAutofill attribute with a value of no to the Password field, as shown in the following example:
<EditText
   android:id="@+id/password"
   ...
   android:importantForAutofill="no"
   ...
   />
  1. Click the Run button.
  2. In your device, tap the Email field and choose a dataset. This time, the Autofill Framework doesn't fill out the Password field, as shown in the following screenshot:
  3. Remove the android:importantForAutofill attribute from the Password field.
  4. Add the android:importantForAutofill attribute with a value of noExcludeDescendants value to the root LinearLayout element, as shown in the following example:
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   ...
   android:importantForAutofill="noExcludeDescendants"
   ...
   tools:context="com.android.developer.myapplication.LoginActivity">
  1. Click the Run button.
  2. In your device, tap the Email field. This time, the Autofill Framework doesn't show any datasets to fill out the screen.
  3. Touch & hold the Email field, tap the overflow menu , choose AUTOFILL.
  4. Android system shows the autofill UI with suggestions to fill out the screen.