cliffroot
cliffroot

Reputation: 1691

Android autofill hints for the username

I have a simple layout with two EditText fields. One for the username and one for the password.

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:importantForAutofill="yes"
        android:inputType="textPassword"
        android:minWidth="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:importantForAutofill="yes"
        android:minWidth="200dp"
        app:layout_constraintBottom_toTopOf="@id/password"
        app:layout_constraintStart_toStartOf="@id/password" />

In the code (onCreate method) I add hints to those EditText views like this

findViewById<View>(R.id.username).setAutofillHints(AUTOFILL_HINT_EMAIL_ADDRESS, AUTOFILL_HINT_USERNAME)
findViewById<View>(R.id.password).setAutofillHints(AUTOFILL_HINT_NEW_PASSWORD)

When I tap the password fiels it suggests creating a password for me, but the username is empty. I understand that there's no way to control what autofill service does and there are many different implementations of them. I want to know if there's a way to make the Google Password Manager specifically to pick up the value from the username field and populate it for me.

Username field is empty

Upvotes: 1

Views: 546

Answers (2)

Saloni Tyagi
Saloni Tyagi

Reputation: 91

You are using AUTOFILL_HINT_USERNAME. you might also try using AUTOFILL_HINT_GENERIC or AUTOFILL_HINT_TEXT for the username field to see if it has any impact

Upvotes: 1

FarshidABZ
FarshidABZ

Reputation: 4123

It seems your code is fine, However, there could be a few reasons why autofill might not be working as expected:

  1. Android Version: Depending on the version of Android your device is running, there are different ways to enable auto-fill.
  2. Device-Specific Settings: Some devices may require a device-specific setting to be enabled for the autofill feature to work.
  3. Battery Optimization Settings: Android battery optimization settings will automatically turn off services (like the Accessibility Service) in order to preserve the battery.

Try to set input type in your XML as well:

<EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:importantForAutofill="yes"
    android:inputType="textEmailAddress"
    android:minWidth="200dp"
    app:layout_constraintBottom_toTopOf="@id/password"
    app:layout_constraintStart_toStartOf="@id/password" />

You can try to use a single, specific autofill hint for each field.

Upvotes: 1

Related Questions