Google introduced Autofill framework 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 not etc. just like web browser.

Benefits of Autofill

Reduced time: Filling form is time consuming process; auto fill framework helps to saves users from retyping the information.

Reduce input errors: Typing is prone to errors, especially in mobile devices. Removing the necessity of typing 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 Google play store.

How to Enable autofill service

To enable autofill service please go through following steps :

Settings > System > Languages & input > Advanced > Input assistance > Autofill service.

How Autofill framework works

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

  • 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. Developer can also developed their own autofill service.
  • 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

How to implement autofill framework

The great thing of 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 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 is the list of constant for autofill hint which are define 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 after API 26. There are many constant are available in android.view.View class with 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 use its heuristics to determine if the view is important for autofill.
  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 its children are not important for autofill.

Following code demonstrate how to set autofill importance
a. You can set the importance 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);