Android How can I detect autofill pressed?

1.1k Views Asked by At

I use AutofillManager https://developer.android.com/reference/android/view/autofill/AutofillManager and can't understand how can I detect is user enter value himself or user select any autofill option. I try to use editText.autofillValue but it contains value both cases

Anyone knows how can I resolve it? Help me, please!)

P.S. code

I have function to request autofill

fun allowAutoFill(view: View?) {
        if (view != null) {
            val afm = requireContext().getSystemService(AutofillManager::class.java)
            if (afm.isAutofillSupported && afm.isEnabled) {
                afm?.requestAutofill(view)
            }
        }
    }

After that i want to know user enter something or select value from autofill. It's needed for analytics.

private fun isAutoFillApplied() = binding?.editPassword?.autofillValue?.textValue?.isNotEmpty() == true

but binding?.editPassword?.autofillValue contains value if user enter something and if user select autofill option

2

There are 2 best solutions below

0
On BEST ANSWER

I couldn't find cool answer, so I used bad dirty hack. I have two edittext in screen. If user select any autofill option this edittexts filled the same time. So if time difference on text changed not so big, I fill isAutofill value as true. This is really not pretty solution, but I can't find another.

This is looks like this

var lastChangedTime = 0L
var isAutoFill = false
var lastRepeatPassword: String? = null

editPassword.addTextChangedListener { text ->
                lastChangedTime = System.currentTimeMillis()
}

editRepeatPassword.addTextChangedListener { text ->
               if (text.toString() != lastRepeatPassword) {
                   lastRepeatPassword = text.toString()
                   isAutoFill = (System.currentTimeMillis() - lastChangedTime) < 50
               }
           }
1
On

I honestly have no idea why developers still request people to repeat a password twice in 2023. I never typed in a password in the last 5 years. But that's besides the point.

AutofillNode has a param called onFill. Use that instead