How do I place an image inside an edittext?
4 answers
I think the best way to approach this is to use action elements. Check the link in the Android Action Bar.
Adding a search view to the action bar is very simple:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
And as explained in the above link, here's how to access it:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Configure the search info and add any event listeners
...
return super.onCreateOptionsMenu(menu);
}
+1
source to share
You can do this programmatically.
EditText editText = findViewById(R.id.myEditText);
// Set drawables for left, top, right, and bottom - send 0 for nothing
editTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.myDrawable, 0);
and
android:drawableRight="@drawable/search"
as suggested by Pragnani.
+1
source to share