6

I know that it is possible to perform a click on a view like this :

view.PerformClick()

How do I do it on TextInputLayout EndIcon button?

Update

The problem is that I have a bunch of InputLayouts and use a generic function to set the click listeners on them like so

fun setTextInputLayoutListeners(
    inputLayout: TextInputLayout, editText: TextInputEditText,
    actionSet: () -> Unit,
    actionClear: () -> Unit
) {
    with (inputLayout) {
        setOnClickListener { actionSet() }
        setEndIconOnClickListener { actionClear() }
    }
    editText.setOnClickListener { actionSet() }
}

and call it with different parameteres like this

setTextInputLayoutListeners(
    categoryInputLayout, categoryEditText, { onCategoryClick() }, { onCategoryClear() }
)
setTextInputLayoutListeners(
    dateInputLayout, dateEditText, { onDateClick() }, { onDateClear(calendar) }
)

so I'm looking for a generic solution, sort of

inputLayout.EndIcon.PerformClick()
yaugenka
  • 2,602
  • 2
  • 22
  • 41

3 Answers3

17
textinput.setEndIconOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do some code
            }
        });

hope it helps..

Jaydeep chatrola
  • 2,423
  • 11
  • 17
3

EndIcon in TextInputLayout is of type CheckableImageButton and its id is R.id.text_input_end_icon (use R.id.text_input_start_icon for StartIcon). To simulate clicks, you need to find the button using findViewById, then cast it as a CheckableImageButton and call performClick().

In your case:

inputLayout.findViewById<CheckableImageButton>(R.id.text_input_end_icon)?.performClick()
  • 1
    Welcome! Providing some context or (short) explanation would help make your answer easier to understand and also more likely to be upvoted. Try to avoid code dump answers or questions. – ymindstorm Jun 01 '21 at 16:53
  • Thank you. It helps me to get end icon view and perform click on it – Ruslan May 01 '22 at 14:44
2

Thank you so much for you clear and helpful answer.I did a little trick that worked and i wanna share my answer to everyone who will need it.

First thing i did is get the string value of my view :

Log.d("tag", String.valueOf(v));

i got that : com.google.android.material.internal.CheckableImageButton{ba604a VFED..C.. ...P.... 8,3-104,99 #7f090125 app:id/text_input_end_icon}

and like i suspected then icon is a different view of the text field layout with different id (in the end of the string value of the view). So i changed my if condition from if (v.getId() == R.id.start_date_Layout) to if (v.getId() == R.id.text_input_end_icon), and it work now

i hope this answer will be helpful to someone, thank you again for all your answer

Khalil Ktiri
  • 181
  • 1
  • 6