user1780366
user1780366

Reputation:

How to place image inside the edittext?

I want to place implement search in my app for that I need to make a search bar like we see in the google bar image at the end of the text field. I have tried using FrameLayout and I place EditText and image, but it doesn' work ..How do I make it.

Upvotes: 3

Views: 444

Answers (4)

Govil
Govil

Reputation: 2054

Programatically you can do this.

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

or also

android:drawableRight="@drawable/search"

as suggested by Pragnani.

Upvotes: 1

Rahim
Rahim

Reputation: 740

I think the best way to approach this is using action items. Check out this link on Android action bar.

Adding a search view into 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 mentioned link here is 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);
}

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

Use this inside your EditText

android:drawableRight="@drawable/search"

this will place image inside your EditText..I hope this will help you..

Upvotes: 1

Sam
Sam

Reputation: 86948

Simply use a RelativeLayout to place a small ImageView on top of your EditText. Don't forget to adjust the EditText's padding so the text doesn't appear under the ImageView.

Upvotes: 2

Related Questions