This question already has an answer here:
In my app I have a EditText
with a search Icon on the right side. I used the code given below.
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dip"
android:layout_weight="1"
android:background="@drawable/textfield_search1"
android:drawableLeft="@drawable/logo"
android:drawableRight="@drawable/search_icon"
android:hint="Search Anything..."
android:padding="4dip"
android:singleLine="true" />
I want to set the onClickListener
for the search icon image assigned to the right drawable
of EditText
. How is it possible?
This has been already answered but I tried a different way to make it simpler.
The idea is using putting an
ImageButton
on the right ofEditText
and having negative margin to it so that theEditText
flows into theImageButton
making it look like the Button is in theEditText
.Also, as shown above, you can use a
paddingRight
of similar width in theEditText
if you don't want the text in it to be flown over theImageButton
.I guessed margin size with the help of android-studio's layout designer and it looks similar across all screen sizes. Or else you can calculate the width of the
ImageButton
and set the margin programatically.Simple Solution, use methods that Android has already given, rather than reinventing wheeeeeeeeeel :-)
I know this is quite old, but I recently had to do something very similar, and came up with a much simpler solution.
It boils down to the following steps:
See this post for the full example: Handling click events on a drawable within an EditText
Please use below trick:
You don't have access to the right image as far my knowledge, unless you override the
onTouch
event. I suggest to use aRelativeLayout
, with oneeditText
and oneimageView
, and setOnClickListener
over the image view as below:Also Create an Interface with
Still if u need any help, comment
Also set the drawableClickListener on the view in activity file.