The Autofill framework introduced by Google from Android Oreo i.e. 8.0 (API 26) and later. With the help of autofill framework user can store their username, password, email, and credit card number, etc. just like a web browser.

Benefits of Autofill

Reduced time: Filling form is a time-consuming process; autofill framework helps to saves users from retyping the information.

Reduce input errors: Typing is prone to errors, especially in mobile devices. Using this it is not necessary to type information also removes the errors that come with it.

Prerequisites

  • OS Version: Android 8.0 or later
  • Autofill Service: Autofill service must be configured in your device. Most of android 8.0 phone have autofill service. You can develop your own android autofill service. There are some third-party autofill services are also available in the Google play store.

How to Enable autofill service

To enable autofill service please go through following steps :

  1. Settings
  2. System
  3. Languages & input
  4. Advanced
  5. Input assistance
  6. Autofill service.

How Autofill framework works

Following are component which are plays vital role in Android Autofill framework

  • Autofill service — An autofill service app installed in your device that can respond to autofill requests and save the data which is entered by a user. User can activate only one app at a time. This service 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.
  • Android system — Android system is a bridge between autofill client and autofill service. The Android system request to the auto-fill service and fills out views in client apps with the data which is provided by the autofill service

How to implement autofill framework

The great thing about autofill framework is that you don’t need to write a single line of code to implement it, Autofill service attempt to determine all views using the experience to learn and improve. But due to this behavior app work unexpectedly. To ensure that the Autofill service correctly identifies your app's forms, you should provide Autofill hints.
Following code demonstrate how to set hint for autofill service.
a. You can set these hints using theandroid:autofillHints attribute:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autofillHints="password" />

b. You may also set them using the setAutofillHints() method:

TextView password = findViewById(R.id.password);
password.setAutofillHints(View.AUTOFILL_HINT_PASSWORD);

It’s a good practice to always provide autofill hint with View.
Following are the constants for autofill hint which are defined in android.view.View class

  1. AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY : Hint indicating that this view can be autofilled with a credit card expiration day.
  2. AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH : Hint indicating that this view can be autofilled with a credit card expiration month.
  3. AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR : Hint indicating that this view can be autofilled with a credit card expiration year.
  4. AUTOFILL_HINT_CREDIT_CARD_NUMBER : Hint indicating that this view can be autofilled with a credit card number.
  5. AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE : Hint indicating that this view can be autofilled with a credit card security code.
  6. AUTOFILL_HINT_EMAIL_ADDRESS : Hint indicating that this view can be autofilled with an email address.
  7. AUTOFILL_HINT_NAME : Hint indicating that this view can be autofilled with a user's real name.
  8. AUTOFILL_HINT_PASSWORD : Hint indicating that this view can be autofilled with a password.
  9. AUTOFILL_HINT_PHONE : Hint indicating that this view can be autofilled with a phone number.
  10. AUTOFILL_HINT_POSTAL_ADDRESS : Hint indicating that this view can be autofilled with a postal address.
  11. AUTOFILL_HINT_POSTAL_CODE : Hint indicating that this view can be autofilled with a postal code.
  12. AUTOFILL_HINT_USERNAME : Hint indicating that this view can be autofilled with a username.

How to set field is important for Autofill or not.

This can be set using android:importantForAutofill attribute, which is available from API 26. There are many constants are available in android.view.View class with the help of them autofill importance is set. By default view uses the IMPORTANT_FOR_AUTOFILL_AUTO mode, In this mode android system determine the view is important for autofill.

  1. auto: Let the Android System determine if the view is important for autofill based on previous.
  2. no: This view is not important for autofill.
  3. noExcludeDescendants: This view, and its children, are not important for autofill.
  4. yes: This view is important for autofill.
  5. yesExcludeDescendants: This view is important for autofill, but it's children are not important for autofill.

Following code demonstrate how to set autofill importance
a. You can set the importance of the field using the android:importantForAutofill attribute:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:importantForAutofill="no" />

b. You can also use the setImportantForAutofill() method:

TextView captcha = findViewById(R.id.captcha);
captcha.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);

Reference : Google Developer