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?
I think it is much more easier if we use some tricks :)
Done
its working for me,
I would like to suggest a way for drawable left! I tried this code and works.
I have created a simple custom touch listener class instead of a custom EditText
}
There will be no drawable when the EditText is blank. A drawable will show when we started editing for clear the EditText.
You can just set the touch listener
mEditText.setOnTouchListener(new MyTouchListener(mEditText));
This works fro me:) may this help you as well
I created a useful abstract class DrawableClickListener which implements OnTouchListener.
In addition to the DrawableClickListener class, I also created 4 additional abstract classes which extend the DrawableClickListener class and handle the clicking of the drawable area for the correct quadrant.
Point to Consider
One thing to consider is that the images are not resized if done this way; thus the images must be scaled correctly before being put into the res/drawable folder(s).
If you define a LinearLayout containing an ImageView and a TextView, it's a lot easier to manipulate the size of the image being displayed.
activity_my.xml
MyActivity.java
DrawableClickListener.java