Skip to content Skip to sidebar Skip to footer

How To Perform Click On TextInputLayout EndIcon Button

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

Solution 1:

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

hope it helps..


Solution 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


Solution 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()

Solution 4:

view.setOnTouchListener(new View.OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
          final int DRAWABLE_LEFT = 0;
          final int DRAWABLE_TOP = 1;
          final int DRAWABLE_RIGHT = 2;
          final int DRAWABLE_BOTTOM = 3;

          if(event.getAction() == MotionEvent.ACTION_UP) {
             if(event.getRawX() >= (quantity.getRight() - quantity.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                 // your action here
                 // For example:
                 view.getText().clear();
                 return true;
              }
          }
          return false;
      }
});

Post a Comment for "How To Perform Click On TextInputLayout EndIcon Button"