Android Searchview with background hint-ThrowExceptions

Exception or error:

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.

How to solve:

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

###

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

###

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

###

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" />

###

These are the important lines which work together:-

       app:iconifiedByDefault="false"

       app:queryHint="@string/crop_name"

###

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>
    

###

Try this.

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

Leave a Reply

Your email address will not be published. Required fields are marked *