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:
- Why is this 'Passwords' field appearing?
- How can I hide the 'Passwords' field if the user does not have a phone number in their account? (If that is the reason)
- Is there a better way to go about doing this?
Any help would be greatly appreciated.
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"
/>
It depends on what your autofill service is providing to your app.
& 3. You can set autofillHints programmatically.
Reference: Optimize your app for autofill