How to add icon inside EditText view in Android ?


Question

I want to add a "search" icon to appear inside an EditText in the left edge? such as search box in Facebook Android app?

1
130
11/8/2013 7:14:36 AM

Accepted Answer

Use the android:drawableLeft property on the EditText.

<EditText
    ...     
    android:drawableLeft="@drawable/my_icon" />
402
11/26/2010 12:38:35 AM

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);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon