Description:
Changing endIconMode of a TextInputLayout (combination with TextInputEditText) does not work dynamically.
To reproduce:
Set a textbox endIconMode first to "clear".
After that set endIconMode to "custom" and set a custom icon drawable - produces strange results.
The end icon is not visible anymore.
After pressing/tapping the area where the endIcon should reside, the icon is visible for a short period of time (milliseconds) showing the changed icon.
Expected behavior: endIconMode should change dynamically without causing it to break endIcon functionality
Source code:
Initial state:
textBox.endIconMode="clear"
Later (change from an event outside / button click / something else):
textBox.endIconMode="custom"
textBox.endIconDrawable = someDrawable
Android API version: 28
Material Library version: 1.1.0-alpha09
Device: Samsung Galaxy S9 / Emulator
I too ran into this issue.. I have an exposed drop down menu using AutoCompleteTextView inside TextInputLayout.
After the user select an item from the drop down.. I set the endIcon to END_ICON_CLEAR_TEXT to give the user the option to clear his selection. Works perfect until this.. When it is cleared, I want to set the endIcon to END_ICON_DROPDOWN_MENU... It doesnt show.. Tried adding a TextWatcher but nothing helped
Also seems like setEndIconCheckable method is not called after new endIcon was set. So if it was END_ICON_CLEAR_TEXT before, END_ICON_PASSWORD_TOGGLE will not work properly, until you will call setEndIconCheckable.
I have the same problem, I'm trying to change between CUSTOM and CLEAR_TEXT modes depending on whether the TextInputEditText is empty or not. So CUSTOM when empty and CLEAR_TEXT otherwise.
What I found is that, when the TextInputEditText is cleared (by pressing the Clear Text button or by deleting the text with the keyboard), the Clear Text button disappears using an animation, which sets its alpha to 0. So when the mode is changed to CUSTOM and the icon replaced, the animation is not stopped and it ends up hiding the new icon. Forcing the button's alpha to go back to 1 _after_ the animation ends works. However, because the animation will still run, the icon will "blink".
Is there any update on this fix ?
i still got some strange behavior when switching dynamically between endIconMode clear_text and dropdown_menu using 1.3.0-alpha02 .
i tried the following implementation, if an item is selected i want the endIconMode to be clear_text if the input is empty i want dropdown_menu.
setting dropdown_menu after clear_text was clicked, leads to an invisible icon because of the clear_text icon animation.
when calling showDropdown() after the clear icon was clicked, and selecting an item, the clear icon is also invisible even the input contains text. nevertheless the icon is clickable, so i assume this is also a problem with the alpha animation
I had the same issue. My solutions was quite simple: implement clear_text behavior with mode=CUSTOM_ICON. It'll look like this
textInputLayout.endIconMode = TextInputLayout.END_ICON_CUSTOM // may be set in xml
textInputLayout.setEndIconDrawable(com.google.android.material.R.drawable.mtrl_ic_cancel)
textInputLayout.setEndIconOnClickListener { textInputLayout.editText?.text?.clear() }
I'm not sure it'll work in all possible cases. It's better to _check ClearTextEndIconDelegate before doing any tricks_ with textInputLayout.
The main problem of the ClearTextEndIconDelegate is internal animations. Because of them it's impossible to call textInputLayout#setEndIconVisible right after switch between end icon modes. iconInAnim#onAnimationEnd will hide end icon in a few ms (exact animation time depends on a device settings).
If ClearTextEndIconDelegate is required and can't be replaced, it's possible to remove all listeners from iconInAnim by using reflection. In this case setEndIconVisible(false) won't be invoked and end icon will stay on screen, but you will need to add proguard/r8 rules to make sure everything is okay in release builds.
Also there is an opportunity to create a custom TextInputLayout with overridden setEndIconVisible and analyze a call stack to determinate when ClearTextEndIconDelegate.iconInAnim.onAnimationEnd invokes TextInputLayout#setEndIconVisible. Invocation of setEndIconVisible(false) from iconInAnim may be ignored to prevent end icon disappear.
Most helpful comment
I have the same problem, I'm trying to change between CUSTOM and CLEAR_TEXT modes depending on whether the
TextInputEditTextis empty or not. So CUSTOM when empty and CLEAR_TEXT otherwise.What I found is that, when the
TextInputEditTextis cleared (by pressing the Clear Text button or by deleting the text with the keyboard), the Clear Text button disappears using an animation, which sets its alpha to0. So when the mode is changed to CUSTOM and the icon replaced, the animation is not stopped and it ends up hiding the new icon. Forcing the button's alpha to go back to1_after_ the animation ends works. However, because the animation will still run, the icon will "blink".