'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:[1]

there are two ways to handle this

  1. SMS read Permission(not recommended)

you can create a popup and give permission when the user touch it.

  1. SMS retriever API( recommended)

you can set SMS retriever API (look at this link) and create a popup when user touch it then fill the text view

Solution 2:[2]

Did you enable autofill in the Android settings?

  • First select an autofill service, e.g. Google.
  • Settings -> Google -> Autofill -> SMS Verification codes -> Switch to enabled

See: https://www.techrepublic.com/article/how-to-enable-sms-verification-code-autofill-in-android/

Solution 3:[3]

After a lot of searches and losing hope in doing it, I found that it was very simple just you set it important for autofill

Edit text code

<EditText
    android:id="@+id/otp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_margin="30dp"
    android:textColor="@color/black"
    android:autofillHints="smsOTPCode"
    android:importantForAutofill="yes"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/status" />

and I did set the autofill hint in code also

lateinit var statusView: TextView
lateinit var otpView: TextView
lateinit var acceptView: RadioButton
lateinit var rejectView: RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    statusView = findViewById<TextView>(R.id.status)
    otpView = findViewById<TextView>(R.id.otp)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        otpView.setAutofillHints(AUTOFILL_HINT_SMS_OTP)
    }
    acceptView = findViewById<RadioButton>(R.id.accept)
    rejectView = findViewById<RadioButton>(R.id.reject)
    startListeningForSms(this,this)
    Log.e(javaClass.simpleName, AppSignatureHelper(this).appSignatures.toString())
}
fun startListeningSms(
    context: Context,
    lifecycleOwner: LifecycleOwner
) {
    val client = SmsRetriever.getClient(context)

    // Starts SmsRetriever, which waits for ONE matching SMS message until timeout
    // (5 minutes). The matching SMS message will be sent via a Broadcast Intent with
    // action SmsRetriever#SMS_RETRIEVED_ACTION.
    val task: Task<Void> = client.startSmsRetriever()

    // Listen for success/failure of the start Task. If in a background thread, this
    // can be made blocking using Tasks.await(task, [timeout]);
    task.addOnSuccessListener(OnSuccessListener<Void?> {
        // Successfully started retriever, expect broadcast intent
    })

    task.addOnFailureListener(OnFailureListener {
        // Failed to start retriever, inspect Exception for more details
    })
}

I'm not sharing the whole activity code, I'm just sharing code related to autofill.enter image description here

AppSignatureHelper can be found here : https://github.com/googlearchive/android-credentials/blob/master/sms-verification/android/app/src/main/java/com/google/samples/smartlock/sms_verify/AppSignatureHelper.java

and autofill library can be added in dependancy

implementation "androidx.autofill:autofill:1.1.0"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 saeedata
Solution 2 Werner Altewischer
Solution 3