Share
Embed
Transcript
14 slides
-
Rules for a good search experience —You need to provide
good content —It has to be fast —It has to be simple -
Persistent Search Use persistent search when search is the primary
focus of your app. -
Expandable Search Display a magnifying glass icon in the toolbar
instead of a search text box. -
Voice Search —When entering text, allow users to either use
the keyboard or their voice. —Let Android handle voice recognition —Voice search is a great feature, but very error prone (and sometimes frustating) —It submits directly what it is said (so no opportunities for second attempts) —Combine text search and voice search -
SearchView —It is built-in component in the Android framework —It
lives in the Toolbar —When activated, turns your toolbar into a text input field —It can also provide historical results —Available in all Android versions (through AppCompat) -
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater.inflate(R.menu.main_activity_options, menu); MenuItem searchItem
= menu.findItem(R.id.action_search); SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextChange(String newText) { // Text has changed, apply filtering? return false; } @Override public onQueryTextSubmit(String query) { // Perform the final search } }); } -
Searchable Configuration —This is a configuration file for search settings:
Info <searchable android:label="@string/app_name" android:hint="@string/search_hint" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/> -
AndroidManifest.xml <activity android:name=".SearchableActivity" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data
android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> -
onCreateOptionsMenu() SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE); ComponentName componentName = new ComponentName(context,
SearchableActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName)); -
Getting the search query Call this from onCreate() or onNewIntent().
private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); } }