I want to add a "search" icon to appear inside an EditText
in the left edge? such as search box in Facebook Android app?
Asked
Active
Viewed 2.2e+01k times
157

Irfan DANISH
- 8,349
- 12
- 42
- 67

Adham
- 63,550
- 98
- 229
- 344
6 Answers
482
Use the android:drawableLeft
property on the EditText.
<EditText
...
android:drawableLeft="@drawable/my_icon" />

marcosbeirigo
- 11,098
- 6
- 39
- 57
-
3sorry for unclear question , i want to add a button , so wheni click on it , it do some action – Adham Nov 26 '10 at 00:38
-
22This was just the solution I expected, given the search heading. – Thomas Ahle Dec 18 '10 at 22:35
-
37Also, adjust the padding using the `drawablePadding` property. – Ryan R Jan 20 '13 at 01:17
-
so what should be the size of that drawable icon and in which folder should i place it? – Kalpesh Lakhani Jul 22 '14 at 06:13
-
2@KalpeshLakhani : Usually the edittext and drawables should be 32dp according to the android design guidelines: http://developer.android.com/design/style/metrics-grids.html – Lee Yi Hong Jul 23 '14 at 02:39
-
@marcosbeirigo This didn't work for me. its filling the whole edit text in a weird way. – Eswar Jul 20 '18 at 14:10
-
use drawableStart for RTL support – Ahmad Shahwaiz Apr 12 '20 at 12:49
35
You can set drawableLeft in the XML as suggested by marcos, but you might also want to set it programmatically - for example in response to an event. To do this use the method setCompoundDrawablesWithIntrincisBounds(int, int, int, int):
EditText editText = findViewById(R.id.myEditText);
// Set drawables for left, top, right, and bottom - send 0 for nothing
editTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);

Community
- 1
- 1

Peter Ajtai
- 56,972
- 13
- 121
- 140
-
1This sets background to the EditText, not the icon/image at the "Left Edge" which is clearly mentioned in the question. – Paresh Mayani Nov 18 '11 at 19:09
-
@Paresh - which is why I deleted the answer. I have now edited to include a programmatic solution in addition to the original XML solution. – Peter Ajtai Nov 18 '11 at 19:26
-
1
-
1@Hunt - Just set the coumound drawable to `null` when the view is clicked. – Peter Ajtai Apr 18 '12 at 16:55
33
If you want to use Android's default drawable, you can use @android:drawable/ic_menu_search
like this:
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Search product.."
android:inputType="textVisiblePassword"/>

Jonathan
- 2,244
- 1
- 23
- 29
0
Use a relative layout and set the button the be align left of the edit text view, and set the left padding of your text view to the size of your button. I can't think of a good way to do it without hard coding the padding :/
You can also use apk tool to sorta unzip the facebook apk and take a look at its layout files.

Nathan Schwermann
- 31,285
- 16
- 80
- 91
-
1I would like to mention that decompilation is against the law in many countries for example in whole European Union so please be carefull :) – Paweł Byszewski Dec 05 '14 at 08:50
0
` <EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/white"
android:drawableStart="@android:drawable/ic_menu_search"
android:hint=" Search" />`

Hariram Ramswarup
- 57
- 1
- 4