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:
Autofill service—An app installed in the device that can respond to autofill requests and save user data. There can only be one active autofill service at a time. Autofill services are usually provided by password manager apps.
Autofill client—Any app installed in the device that contains views that can be filled out with user data. It's also an app where users type data that a service can store for later use.
Android system—Manages the interaction between the service and clients. The Android system routes autofill requests to the service and fills out views in client apps with the user data provided by the autofill service.
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:
Configure a test autofill service in your device.
Use the test autofill service to save data that you can use in your tests.
Build a simple app for Android that includes an activity with the following fields:
Email
Password
Captcha
Provide the following optimizations for the simple app:
How to validate that the optimizations work with the autofill service.
What you'll need
A device running Android 8.0 (API level 26) or higher. The codelab assumes that the device is an emulator, but you can also use a hardware device connected to your development machine using a USB cable. For more information, see Run Apps on a Hardware Device.
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:
Open Android Studio and click Import project (Gradle, Eclipse ADT, etc.)
In the Select Eclipse or Gradle Project to Import dialog, choose the folder where you downloaded the autofill service sample. Click OK.
Select afservice from the Run/Debug configuration dropdown and click the Run button.
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:
Open the Settings app in your device.
Open the System > Languages & Input > Advanced > Autofill service option and choose the Multi-Dataset Autofill Service.
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:
Open Autofill Settings from the app launcher.
Tap Add fake Autofill data, and select the number of datasets that you would like to use for testing.
Create the client app
In Android Studio, click File > New > New Project...
In the Create Android Project dialog click Next.
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.
In the Add an Activity to Module dialog, choose Login Activity. Click Next.
In the Configure Activity dialog, click Finish.
Test the client app without optimizations
In Android Studio, open the client app project.
Click the Run button.
Tap the Email field. The Autofill Framework doesn't offer any suggestions to fill out the form.
Provide autofill hints attributes to client views
In Android Studio, open the client app project.
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:
Test the client app with autofill hints optimizations
In Android Studio, open the client app project.
Click the Run button.
Tap the Email field. Note that the Autofill Framework offers suggestions to fill out the screen, as shown in the following screenshot:
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.
In Android Studio, open the client app project.
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:
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:
Remove the android:importantForAutofill attribute from the Password field.
Add the android:importantForAutofill attribute with a value of noExcludeDescendants value to the root LinearLayout element, as shown in the following example: