vignesh s
vignesh s

Reputation: 203

Android Searchview with background hint

I am a newbie to android development, and would like to know if it is possible to create an search view with background hint, which changes automatically, as the user types in the search view box. for example, like in the Google search, where the grey part in the background gives the user the options of filling in.

Upvotes: 20

Views: 35936

Answers (7)

Kaveri
Kaveri

Reputation: 1088

These are the important lines which work together:-

       app:iconifiedByDefault="false"

       app:queryHint="@string/crop_name"

Upvotes: 8

AlexM
AlexM

Reputation: 1135

The easiest way to view a hint is by adding queryHint="Search" attribute to SearchView widget in XML.

<android.support.v7.widget.SearchView
    android:id="@+id/svSearchView"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    app:queryHint="Search products.."
    app:iconifiedByDefault="false"
    android:layout_below="@+id/tvTitle"
    android:paddingLeft="14dp" />

Upvotes: 7

Shamsul Arafin Mahtab
Shamsul Arafin Mahtab

Reputation: 559

Try this.

SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setIconified(false);

Upvotes: 1

Shashanth
Shashanth

Reputation: 5180

You can add SearchView hint in two ways

  1. By programmatically

    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setQueryHint("Search Hint");
    
  2. By using theme (styles.xml)

    <style name="MySearchView" parent="Widget.AppCompat.SearchView">
        <item name="queryHint">@string/search_view_hint</item>
    </style>
    

Upvotes: 2

lokoko
lokoko

Reputation: 5803

Try something like this :

SearchView searchView = new SearchView(canvas.getContext());
searchView.setQueryHint("Type your text here");
searchView.setBackgroundColor(Color.WHITE);

OptionCommand searchCommand = new OptionCommand();
searchCommand.setActionView(searchView);
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

Upvotes: 13

Balaji
Balaji

Reputation: 2026

You can add SearchView hint by following below :

search.setQueryHint("Google Search");

You can set Background color of SearchView by following below :

search.setBackgroundColor(Color.RED);

Upvotes: 47

Juozas Kontvainis
Juozas Kontvainis

Reputation: 9597

Yes, you can read more about it here: http://developer.android.com/guide/topics/search/search-dialog.html

Upvotes: 2

Related Questions