3

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

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Attique Ur Rehman
  • 336
  • 1
  • 2
  • 19
  • 1
    If you have received OTP then why are you suggesting user to add the OTP. I mean instead of asking user to set the otp just set the otp automatically. – Rajendra Mahato Jan 02 '21 at 07:29
  • Application can't fetch otp auto from user messages, i want some mechanism for this. I'm not using firebase. SMS is sent from my server using twillio. and Android is not allowing reading users sms only to verify otp. – Attique Ur Rehman Jan 02 '21 at 07:56
  • is Twillio providing some on success or receive listeners(call backs? – Rajendra Mahato Jan 02 '21 at 08:33

4 Answers4

5

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.

Burak Dizlek
  • 4,805
  • 2
  • 23
  • 19
2

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

saeedata
  • 901
  • 6
  • 14
  • The pop-up in above shared image is any how displayed by android OS, i want to know the trick behind it – Attique Ur Rehman Jan 02 '21 at 10:13
  • if you mean the icon of the current messaging applications, you can get it from package manager and filter them by mime_type like "vnd.android-dir/mms-sms" and find the currently active messaging application. look at this link https://stackoverflow.com/questions/15567957/how-to-get-running-applications-icon-on-android-programmatically – saeedata Jan 02 '21 at 10:27
  • look at this link bro,https://stackoverflow.com/questions/8711940/how-to-open-and-show-phones-message-inbox-programmatically – saeedata Jan 02 '21 at 10:31
  • I think you can get your answer by merging code from two links above and also Sms retiver API – saeedata Jan 02 '21 at 10:32
  • 1
    Yes, i found one more accurate ways, merging sms retiver api and twillio to achieve required behaviour. This approach will not require SMS read permission. https://www.twilio.com/docs/sms/app-verification#verify-android-phone-numbers-with-twilio – Attique Ur Rehman Jan 02 '21 at 10:37
1

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/

Werner Altewischer
  • 10,080
  • 4
  • 53
  • 60
-2

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"
Islam Assem
  • 1,376
  • 13
  • 21