3

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.

  • 1
    You may have referred to [this question](http://stackoverflow.com/q/3703283/1267661) with "I have tried using FrameLayout...", but if you haven't please look over these answers as well. – Sam Feb 05 '13 at 18:47

4 Answers4

2

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.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • really great link....I should have search before asking question..Thanks for help.... –  Feb 05 '13 at 18:51
1

Use this inside your EditText

android:drawableRight="@drawable/search"

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

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • but unforutnately I can Accept only one answer...upvote for spending your time –  Feb 05 '13 at 18:55
1

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);
}
Rahim
  • 721
  • 7
  • 13
1

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.

Govil
  • 2,034
  • 20
  • 20
  • Thanks @gOVIL...but unforutnately I can Accept only one answer...I'll give you upvotes –  Feb 05 '13 at 18:54