How to add icon inside EditText view in Android ?

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Use the android:drawableLeft property on the EditText.

<EditText
	...		
	android:drawableLeft="@drawable/my_icon" />

Solution 2 - Android

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

Solution 3 - Android

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"/>

Solution 4 - Android

Use :

<EditText
    ..     
    android:drawableStart="@drawable/icon" />

Solution 5 - Android

use android:drawbleStart propery on EditText

    <EditText
        ...     
        android:drawableStart="@drawable/my_icon" />

Solution 6 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdhamView Question on Stackoverflow
Solution 1 - AndroidmarcosbeirigoView Answer on Stackoverflow
Solution 2 - AndroidPeter AjtaiView Answer on Stackoverflow
Solution 3 - AndroidJonathanView Answer on Stackoverflow
Solution 4 - AndroidMaher AbuthraaView Answer on Stackoverflow
Solution 5 - AndroidRohit JakharView Answer on Stackoverflow
Solution 6 - AndroidNathan SchwermannView Answer on Stackoverflow