material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[TextInputLayout] OnFocusChangeListener is only notified on first gained focus

Open mpost opened this issue 4 years ago • 13 comments

Description: Settings a OnFocusChangeListener on the TextInputEditText inside the TextInputLayout notifies the listener only the very first time the focus is gained.

Expected behavior: The listener should always fire when focus is gained and lost.

Source code:

textInputEditText.setOnFocusChangeListener { _, hasFocus -> 
  println("only fires on first focus gained")
}

Android API version: 29

Material Library version: 1.2.0-alpha04

Device: Emulator

mpost avatar Feb 12 '20 15:02 mpost

Hi, I'm not able to reproduce the issue. Could you provide a sample app?

leticiarossi avatar Feb 26 '20 20:02 leticiarossi

Ok, closing this issue.

mpost avatar Feb 28 '20 16:02 mpost

Hi, I have the same issue here. Why this ticket has been closed ? Is there a fix available ? Thanks

kazougagh avatar Aug 03 '20 19:08 kazougagh

@kazougagh can you provide a sample app with the issue since it's not reproducible on our end?

leticiarossi avatar Aug 04 '20 15:08 leticiarossi

Hi @leticiarossi , This issue occurs when you try to set the endIconMode programmatically and not using the xml attributes. Like this:

`

 val inputLayout = findViewById<TextInputLayout>(R.id.text_input_layout)
 inputLayout.editText?.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->

        // ----------------------------
        // Called on first focus only !
        // ----------------------------

        Log.d(TAG, "inputEditText - hasFocus $hasFocus")
        if (inputLayout.endIconMode == TextInputLayout.END_ICON_NONE) {
            inputLayout.endIconMode = TextInputLayout.END_ICON_CLEAR_TEXT
            inputLayout.setEndIconTintList(ColorStateList.valueOf(getColor(R.color.colorPrimaryDark)))
        }
        
    }`

kazougagh avatar Aug 13 '20 18:08 kazougagh

I have the exact same issue, as described by kazougagh. When setting endIconMode programatically its never called again.

acarlsen avatar Sep 02 '20 11:09 acarlsen

Can we reopen this issue since it's still valid and reproducible example was provided by kazougagh?

D-Fox avatar Jan 15 '21 17:01 D-Fox

Also this is relevant to comment in different issue https://github.com/material-components/material-components-android/issues/503#issuecomment-757379005

D-Fox avatar Jan 15 '21 17:01 D-Fox

Both ClearTextEndIconDelegate and DropdownMenuEndIconDelegate set their own OnFocusChangeListener which replaces any OnFocusChangeListener that was previously set on the EditText.

ikim24 avatar Jan 15 '21 21:01 ikim24

I have the same issue with this code. While debugging, the breakpoint only stopped once.

edtStreet.onFocusChangeListener =
            View.OnFocusChangeListener { _, hasFocus ->
                if (!hasFocus && !isValidField(edtStreet.text)) {
                    binding.edlStreet.setEndIconDrawable(R.drawable.ic_error_white)
                    binding.edlStreet.setEndIconOnClickListener { showToast() }
                    binding.edlStreet.endIconMode = END_ICON_CUSTOM
                    binding.edlStreet.isEndIconVisible = true
                } else {
                    binding.edlStreet.setEndIconDrawable(R.drawable.ic_clear_white)
                    binding.edlStreet.endIconMode = END_ICON_CLEAR_TEXT
                    binding.edlStreet.isEndIconVisible = true
                }
            }

silencieuxle avatar Mar 15 '21 10:03 silencieuxle

FWIW, I just thought I'd encountered a similar problem, when I really didn't understand how hasFocus worked.

Your current view will not lose focus unless something else if focused. This means that your parent view, or something else in your layout, should have android:focusableInTouchMode="true". That way, the edittext will actually lose focus when you click outside of the EditText, and your listener should fire just fine.

koalahamlet avatar Jul 21 '21 23:07 koalahamlet

I meet the same problem :(

hxtruong6 avatar Aug 16 '21 03:08 hxtruong6

I have the same problem. Any plans here?

roklyt avatar Aug 12 '22 09:08 roklyt

faced the same resolved by adding getEditText() before setOnfocusChangeListner textinputLayout.getEditText().setOnFocusChangeListener( ............

manumathew17 avatar Sep 30 '22 09:09 manumathew17