Android/Kotlin - adding TextInputLayout.END_ICON_CLEAR_TEXT to textview doesn't perform the setOnFocusChangeListener

623 Views Asked by At

i've a problem in android app written in Kotlin.

I've a textview that has a setOnFocusChangeListener and it worked fine as long as i've added an EndIconMode. Now in my layout i see the END_ICON_CLEAR_TEXT and this works, but nothing happens when this textview loses focus.

How can I made this works?

textInputLayout = LayoutInflater.from(activity).inflate(R.layout.textinputlayouttemplate, null) as TextInputLayout
val textView = CreateUI._TextView(castToField(field), textInputLayout.context)

if (field.validatefield || field.nextpage != 0)
{
    textView.setOnFocusChangeListener { _, hasFocus ->
        if (!hasFocus && !GlobalVar.drawerOpened)
        {
            if ((field.mandatory && !textView.text.toString().isNullOrEmpty()) || (!field.mandatory)) {
                if (field.validatefield) {
                    validateField(field.fieldcode, textView.text.toString(), field.nextpage)
                } else {
                    _goToNextPageNo = field.nextpage
                    goToNextPageIfExist()
                }
            }
        }
    }
}

textInputLayout.addView(textView, layout_wMATCHPARENT_hWRAPCONTENT)
textInputLayout.layoutParams = layout_wMATCHPARENT_hWRAPCONTENT
textInputLayout.setPadding(0, 0, 0, 0)
textInputLayout.endIconMode = TextInputLayout.END_ICON_CLEAR_TEXT
linearLayout.addView(textInputLayout)

enter image description here

enter image description here

1

There are 1 best solutions below

1
woodii On

Not sure if this is still relevant for you but apparently this is because setting the endIconMode programatically will currently override onFocusChangeListener.

You will even have issues if you set this property via xml and set your own onFocusChangeListener. If you not invoke the previous set listener aswell the clear-icon will perform not as intended.

Either you set it via xml and invoke the default listener or use endIconMode="custom" and write your own logic.

Here is a similar issue