I have added an image right of the text in an EditText
widget, using the following XML:
<EditText
android:id="@+id/txtsearch"
...
android:layout_gravity="center_vertical"
android:background="@layout/shape"
android:hint="Enter place,city,state"
android:drawableRight="@drawable/cross" />
But I want to clear the EditText
when the embedded image is clicked. How can I do this?
Solutions above work, but they have side affect. If you have an EditText with right drawable like
you will get a PASTE button after every click at the drawable. See How to disable paste in onClickListener for the Drawable right of an EditText Android (inside icon EditText).
I know this is quite old, but I recently had to do something similar... After seeing how difficult this is, I came up with a much simpler solution:
In my case, I needed an EditText that had the ability to clear the text with a button. I wanted it to look like SearchView, but for a number of reasons I didn't want to use that class. The example below shows how I accomplished this. Even though it doesn't have to do with focus change, the principles are the same and I figured it would be more beneficial to post actual working code than to put together an example that may not work exactly as I intended:
Here is my layout: clearable_edit_text.xml
And here is the Class that inflates that layout: ClearableEditText.java
To make this answer more in line with the question the following steps should be taken:
It is all great but why not to make it really simple?
I have faced with that also not so long ago...and android touchlistiner works great but gives limitation in usage..and I came to another solution and I hope that will help you:
Now you can create ImageClick listener or event and do what ever you want with text. This edittext_round_corners.xml file
Follow below code for drawable right,left,up,down click:
}
Compound drawables are not supposed to be clickable. It is cleaner to use separate views in a horizontal LinearLayout and use a click handler on them.
Here's my simple solution, just place
ImageButton
overEditText
: