Search filter

header

From Google Material Design documentation.

The app bar can act as a text input field. As the user types, the content underneath is filtered and sorted.

How to add?

I. In your build.gradle add latest appcompat library.

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
}

II. Create a searchable configuration in a searchable.xml saved in res/xml directory.

<?xml version="1.0" encoding="utf-8"?>  
<searchable xmlns:android="http://schemas.android.com/apk/res/android"  
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>  

III. Declare your searchable activity in the AndroidManifest.xml file.

<activity android:name=".SearchActivity">  
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>  

IV. Declare your SearchView inside any layout.xml.

<android.support.v7.widget.SearchView  
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

V. Setup your SearchView on your Activity's onCreate method.

SearchView searchView = (SearchView) findViewById(R.id.search);  
// Sets searchable configuration defined in searchable.xml for this SearchView
SearchManager searchManager =  
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));  

VI. Receive a search query.

// When a user executes a search the system starts your searchable activity and sends it a ACTION_SEARCH intent

Intent intent = getIntent();  
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {  
  String query = intent.getStringExtra(SearchManager.QUERY);
  doMySearch(query);
}

Listen for user input

Receiving query text as user types it lets you filter search results faster.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
    @Override
    public boolean onQueryTextSubmit(String query) {
        searchFor(query);
        return true;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        filterSearchFor(query);
        return true;
    }
});

How to style?

style

I. Declare custom styles in your styles.xml file.

<style name="SearchViewTheme" >  
    <item name="colorControlActivated">@color/amber500</item>
    <item name="colorControlNormal">@color/green500</item>
</style>  

II. Apply this style to the SearchView via android:theme attribute. To change search icon use app:searchIcon attribute.

<android.support.v7.widget.SearchView  
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:searchIcon="@drawable/ic_library"
    app:theme="@style/SearchViewTheme"/>

Searchable activity lifecycle

  1. If SearchView is not in the searchable activity: normal activity lifecycle events are triggered once search is executed. As a result, current activity is paused and your searchable activity is created.

  2. If SearchView is in the searchable activity, then one of these two things happends:

    1. Default: searchable activity receives a call to onCreate with an ACTION_SEARCH intent. This will create two instances of your searchable activity (one on top of the other).

    2. If you set android:launchMode to singleTop: searchable activity receives a call to onNewIntent with an ACTION_SEARCH intent.

Want to know more? Check out Google Search Overview documentation.