androidsearchviewandroid-searchgoogle-voice-search

Android searchable configuration voice search language


I am implementing voice search in my activity using searchable configuration as defined here. However setting android:voiceLanguage="tr" doesn't seem to work if the locale of the device is different. The search application dialog opens in device's locale as well as the search result. I use SearchView, therefore can't catch the onSearchRequested function, otherwise I could specify parameters programmatically. The device locale is in Russian so search queries are returned in Cyrillic. How can I force search results to be returned in Turkish or at least English locale?

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:label="@string/app_name"
  android:voiceLanguage="tr"
  android:voiceLanguageModel="web_search"
  android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />

searchview setup

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    searchItem = menu.findItem(R.id.search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getString(R.string.search));
    searchView.setOnQueryTextListener(new SearchWatcher());
    if (searchManager != null)
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    return true;
}

handling search results

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        searchView.setQuery(query, false);
    }
}

Solution

  • After digging out a little bit I found that SearchableInfo class has following property

    private final int mVoiceLanguageId;
    

    which means that they expect you to pass String resource in your searchable.xml file. Therefore, specifying voiceLanguage from resources solves the problem.

    android:voiceLanguage="@string/locale"