How do I place an image inside an edittext?

I want to place a search in my application for this, I need to create a search bar as we see in the image as a string at the end of the text box. I tried to use FrameLayout and I put EditText and Image, but it doesn't work. How do I do it.

+3


source to share


4 answers


Use this inside your EditText

android:drawableRight="@drawable/search"

      



this will put the image inside your EditText .. I hope this helps you.

+1


source


Just use a RelativeLayout to place a small ImageView on top of your EditText. Remember to adjust the EditText padding so that the text does not appear below the ImageView.



+2


source


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


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







All Articles