Search code examples
androidvoicesearchview

SearchView Voice Recognizer Does Not Transcribe Text


I setup a simple Android project with one activity that contains a SearchView with Voice Search enabled. When I utilize the Voice Search feature, the built-in Voice Recognizer displays as expected, but it does not transcribe the spoken text in either the recognizer dialog or the SearchView widget itself. However, it does correctly send the spoken text through the SEARCH intent so the search can be performed. Here's the source code and XML for my project:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="searchtest.screens"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name="searchtest.screens.MainScreen"
        android:label="@string/app_name"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable_deal" />
    </activity>
</application>

</manifest>

searchable_deal.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_deals_hint"
android:label="@string/app_name"
android:voiceLanguageModel="web_search"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable>

main_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dealScreenLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/searchLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <SearchView
        android:id="@+id/dealSearch"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="@string/search_deals_hint" >
    </SearchView>
</LinearLayout>

<ListView
    android:id="@+id/dealList"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight=".8" >
</ListView>
</LinearLayout>

MainScreen.java

public class MainScreen extends Activity implements OnItemClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    setupSearchView();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // use an inflater to populate the ActionBar with items
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.view_deals, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return true;
}

@Override
protected void onNewIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        String query = intent.getStringExtra(SearchManager.QUERY);
        doSearch(query);
    }
}

private void setupSearchView() {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) findViewById(R.id.dealSearch);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(searchableInfo);
    searchView.setIconifiedByDefault(false);
}

private void doSearch(String query) {

    Toast.makeText(this, "Searching for: " + query, Toast.LENGTH_LONG).show();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Toast.makeText(this, "Selected item: " + position, Toast.LENGTH_LONG).show();
}
}

Solution

  • Please read the note below:

    Note: Carefully consider whether voice search is appropriate for your application. All searches performed with the voice search button are immediately sent to your searchable activity without a chance for the user to review the transcribed query. Sufficiently test the voice recognition and ensure that it understands the types of queries that the user might submit inside your application.

    http://developer.android.com/guide/topics/search/search-dialog.html#VoiceSearch