How to add icon inside EditText view in Android ?

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 Studio gradle doesn't compile the specified version
  • How to achieve ripple animation using support library?
  • Android Export signed application package error
  • How to handle a webview confirm dialog?
  • Android: Handling back button during asynctask
  • Set activity background in theme using a style?
  • How to transfer files between Android applications running on the same device?
  • Styling the popup menu in Android 5.0
  • Android NDK / JNI - undefined reference to function defined in custom header file
  • Android SSO (Single sign-on) for app
  • Where is the location of file created by Cordova File Plugin?
  • RecyclerView Adapter notifyDataSetChanged stops fancy animation
  • 5 Solutions collect form web for “How to add icon inside EditText view in Android ?”

    Use the android:drawableLeft property on the EditText.

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

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

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

    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.

    Use :

    <EditText
        ..     
        android:drawableStart="@drawable/icon" />
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.