Android Autofill Phone Number not appearing, only showing 'Passwords'

1.4k Views Asked by At

I am trying to utilize the Android's Autofill feature in an EditText field to give the user the suggestion of using the phone number associated with their device/account at the top of the keyboard as shown in images below.

I tried to add android:autofillHints="phone" and android:autofillHints="phoneNumber" into the xml file for the layout. Both of these options resulted in the left screenshot where this 'Passwords' button appears. (I also tested this code on an actual device instead of an emulator)

To see if I wasn't using autofill correctly or if this was a device/account issue, I tried changing the code to android:autofillHints="emailAddress". That resulted in the right screenshot that suggests the account's email address (redacted for privacy). This leads me to believe that there might not be a phone number saved on my account or something which is why it defaults to 'Passwords'.

My questions are:

  1. Why is this 'Passwords' field appearing?
  2. How can I hide the 'Passwords' field if the user does not have a phone number in their account? (If that is the reason)
  3. Is there a better way to go about doing this?

Any help would be greatly appreciated.

Autofill Results

Here is the xml code for the edit text:

<EditText
    android:id="@+id/appt_type_phone_number_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autofillHints="phone"
    android:importantForAutofill="yes"
    android:background="@android:color/transparent"
    android:ems="10"
    android:hint="@string/enter_phone_number"
    android:inputType="phone"
    android:maxLength="16"
    android:textSize="14sp"
    android:padding="16dp"
    tools:targetApi="O"
    />
1

There are 1 best solutions below

1
On
  1. It depends on what your autofill service is providing to your app.

  2. & 3. You can set autofillHints programmatically.

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         binding = ActivityMainBinding.inflate(layoutInflater)
         setContentView(binding.root)
         binding.appt_type_phone_number_text.setAutofillHints(View.AUTOFILL_HINT_PHONE) }
    

Reference: Optimize your app for autofill