SEARCH FILTER
rom 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/xmldirectory.
<?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);
}
Comments
Post a Comment