androidkotlinautofillandroid-autofill-manager

Android How can I detect autofill pressed?


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


Solution

  • 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
                   }
               }