androidmessageautofillone-time-password

How to add auto fill suggestion for otp


I've seen in some app(don't remember name) where under textbox, a small pop-up is shown saying

Auto fill code from messages

I want to add a similar functionality to my app. As suggested in documentation add auto fill hints and set autofill importance to achieve this behaviour. i've tried both but none worked. I've tried followings

        <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_login"
        android:layout_marginTop="@dimen/x60"
        android:id="@+id/pin"
        app:errorEnabled="true"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_centerHorizontal="true">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="PIN"
            android:drawablePadding="@dimen/x16"
            android:inputType="number"
            android:maxLength="6"
            android:id="@+id/et_pin"
            android:importantForAutofill="yes"
            android:autofillHints=".AUTOFILL_HINT_SMS_OTP"
            android:drawableStart="@drawable/ic_pin"/>
    </com.google.android.material.textfield.TextInputLayout>

I want to have get this type of thing in my app

enter image description here


Solution

  • 1.Add the following to edittext in xml:

     android:autofillHints="smsOTPCode"
     android:importantForAutofill="yes"
    

    2.Enable the autofill service by navigating to Settings > Google > Autofill > Sms verification codes > Autofill service.

    Note: This solution will work on android 8 and above.