1

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

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
  • can you provide some code? – ahmad bajwa Feb 09 '21 at 16:38
  • @ahmadbajwa 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 detect user enter something or select value from autofill. It's needed for analytics. ```private fun isAutoFillApplied() = binding?.editPassword?.autofillValue?.textValue?.isNotEmpty() == true``` – user9139719 Feb 09 '21 at 17:10

1 Answers1

0

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