Search code examples
androidspeech-recognitionvoice-recognitionandroid-tv

Android-TV speech recognition with manual input


I've implementing voice-search on my Android-TV app using the following small code

private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}


@Override
public void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == -1) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        Toast.makeText(getActivity(), spokenText, Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This is all working well and I get the result back for further processing.

The problem is that I also want to give the user the possibility to enter the search-string manually with the virtual keyboard. In Googles own apps you do this be simply pressing RIGHT on the remote to give focus to the textbox after pressing the voice-search icon.

In my example above I can see the "built-in-textbox" when I press the search icon but if I try to navigate to it the search is interrupted and closed.

How do I access the search-textbox? This should cancel voice-input and bring up the keyboard instead, just like Play Store app.


Solution

  • Are you using Leanback support library for your Android TV app design? I guess "Google Play Store app" and "YouTube app" are using BrowseFragment & SearchFragment for search. These Fragments are providing build-in search UI.

    For the implementation, see Google's sample source code or SearchFragment – Android TV app Tutorial 12.