I have two events on EditText:
- Enter Password in EditText
- Eye Button in Drawable Right for Hide or Unhide The Password Text
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our W3Make Forum to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
The first thing you have to do is give the drawable an ID and get its reference in its respective java file, and after that, you have to setOnClickListner on it and implement two methods.
To set an
OnClickListener
for the drawable on the right side of anEditText
, you can follow these steps:EditText
and set thedrawableRight
attribute to the drawable you want to have clickable:xmlCopy code
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/my_drawable" />
EditText
view and set theOnClickListener
on itsdrawableRight
:javaCopy code
EditText editText = findViewById(R.id.editText);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// Click event on drawableRight
// Perform your desired action here
return true; // Consumes the touch event
}
}
return false;
}
});
Note: The code above uses an
OnTouchListener
to detect the touch event on the drawable. It checks if the touch event occurred within the bounds of the drawable on the right side (drawableRight
). If it did, you can perform the desired action. Returningtrue
from theOnTouchListener
indicates that the touch event has been consumed and should not be propagated further.Remember to replace
@drawable/my_drawable
with the actual drawable resource you want to use.If you’re using Kotlin, the code will be similar, but with a few syntax differences. Here’s the Kotlin equivalent:
kotlinCopy code
val editText = findViewById<EditText>(R.id.editText)
editText.setOnTouchListener { v, event ->
val DRAWABLE_RIGHT = 2
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= (editText.right - editText.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
// Click event on drawableRight
// Perform your desired action here
return@setOnTouchListener true // Consumes the touch event
}
}
false
}
By implementing the
OnClickListener
for the drawable right of theEditText
, you can respond to clicks specifically on that drawable and execute your desired code accordingly.In your XML layout file, define the EditText and set the drawableRight attribute to the drawable you want to have clickable:
xml
Copy code
<EditText
android:id=”@+id/editText”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:drawableRight=”@drawable/my_drawable” />
In your Java or Kotlin code, find the EditText view and set the OnClickListener on its drawableRight:
java
Copy code
EditText editText = findViewById(R.id.editText);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (editText.getRight() – editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// Click event on drawableRight
// Perform your desired action here
return true; // Consumes the touch event
}
}
return false;
}
});
Note: The code above uses an OnTouchListener to detect the touch event on the drawable. It checks if the touch event occurred within the bounds of the drawable on the right side (drawableRight). If it did, you can perform the desired action. Returning true from the OnTouchListener indicates that the touch event has been consumed and should not be propagated further.
Remember to replace @drawable/my_drawable with the actual drawable resource you want to use.
If you’re using Kotlin, the code will be similar, but with a few syntax differences. Here’s the Kotlin equivalent:
kotlin
Copy code
val editText = findViewById<EditText>(R.id.editText)
editText.setOnTouchListener { v, event ->
val DRAWABLE_RIGHT = 2
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= (editText.right – editText.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
// Click event on drawableRight
// Perform your desired action here
return@setOnTouchListener true // Consumes the touch event
}
}
false
}
By implementing the OnClickListener for the drawable right of the EditText, you can respond to clicks specifically on that drawable and execute your desired code accordingly.