Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Search Voice

C887ad592770a197f114d0a1d3e3a5a7?s=47 Jorge Coca
December 01, 2015

Android Search Voice

SPR Learning Lunch - Android Voice Search


Jorge Coca

December 01, 2015


  1. Android Voice Search

  2. Rules for a good search experience —You need to provide

    good content —It has to be fast —It has to be simple
  3. Search in Android —Persistent search —Expandable search

  4. Persistent Search Use persistent search when search is the primary

    focus of your app.
  5. Expandable Search Display a magnifying glass icon in the toolbar

    instead of a search text box.
  6. Voice Search

  7. 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
  8. 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)
  9. SearchView as MenuItem <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_action_search" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView"/>

  10. @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 } }); }
  11. 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"/>
  12. 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>
  13. onCreateOptionsMenu() SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE); ComponentName componentName = new ComponentName(context,

    SearchableActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
  14. 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); } }