Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using a SearchView to Search Through Data in a ListView

Author: Ian Darwin
Published? true -- FormatLanguage: W

Problem:

You want a search box (with a magnifying glass icon and an "x" to clear the text) to filter the content displayed in a ListView

Solution:

Use a SearchView in your layout. Call setTextFilterEnabled(true) on the ListView. Call setOnQueryTextListener() on the SearchView, passing an instance of SearchView.OnQueryTextListener. In the listener's onQueryTextChanged() method, pass the argument along to the ListView's setFilterText(), and you're done!

Discussion:

SearchView is a powerful control that is often overlooked in designing list applications. It has two modes, inline and activity-based. In the inline mode which we demonstrate here, it can be easily added to an existing list-based application with minimal disruption. In the activity-based mode, a separate activity must be created to display the results; that is not covered here but is in the official documentation.

The ListView class has a filter mechanism built in. If you enable it and call setFilterText("string"), then only items in the list that contain "string" will be visible. You can call this many times, e.g., as each character is typed, for a dynamic effect.

Assuming you have a working ListView-based application, you need to take the following steps:

  • Add a SearchView to the layout file, and give it an id such as searchView.
  • In your List activity's onCreate(), find the ListView if needed, and call its setTextFilterEnabled(true).
  • In your List activity's onCreate(), find the searchView by id and call several methods on it, the important one being setOnQueryTextListener()
  • In the QueryTextListener's onQueryTextChanged(), if the passed CharSequence is empty, clear the ListView's filter text (so it will display all the entries).

If the passed CharSequence is not empty, convert it to a String and pass it do the ListView's setFilterText() method.

It really is that simple! The following code snippets are taken from a longer ListView example that was made to work with a SearchView by following this recipe.

In the Activity's onCreate():


// Tailor the adapter for the SearchView
mListView.setTextFilterEnabled(true);
	
mSearchView = (SearchView) findViewById(R.id.searchView);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);			  
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setQueryHint(getString(R.string.search_hint));

In the Activity's implementation of SearchView.onQueryTextListener:


public boolean onQueryTextChange(String newText) {
	if (TextUtils.isEmpty(newText)) {
		mListView.clearTextFilter();
	} else {
		mListView.setFilterText(newText.toString());
	}
	return true;
}
public boolean onQueryTextSubmit(String query) {
	return false;
}

It really is that simple to get started! The ListView does the work of filtering; we just need to control its filtering using the two methods called from onQueryTextChange(). But there is also a lot more to the SearchView, so the javadoc page or the developer documentation mentioned below should be consulted.

See Also:

https://developer.android.com/training/search/index.html

Download:

The source code for this project can be downloaded from https://github.com/IanDarwin/TodoAndroid.git.
No records found.