How to override voice input key on Android keyboard?

1.1k views Asked by At

I created an Activity with a button, which when clicked, starts an Intent to launch the voice input as follows:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
startActivityForResult(intent, SPEECH_REQUEST_CODE);

Then, I get the results and display them in a dialog so that the user can select the closest match.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
        final ArrayList<String> list_voice_input = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        // I use this ArrayList to create a dialog.
    }
    super.onActivityResult(requestCode, resultCode, data);
}

So the Activity works as follows: Click the button, speak, and then a dialog pops up listing the closest matches and you can select one.

I have used a button-click to implement this. But Android's keyboard already has inbuilt voice input. When I use it to speak, it types out what I am saying, automatically detecting the closest match.

What I need:

I wish to make use of the default keyboard instead of a button, detecting when the user has chosen to speak instead of type (in a text field), and display a dialog box with the closest matches.

Any ideas on how I can do this?

Edit:

My question was marked as a duplicate, but I am not trying to get rid of the pop-up dialog that comes when you are recording your voice. My question is different.

When you are typing text, in the Android keyboard, there is a voice input option already inbuilt. You can use that to speak text. So, my question is, instead of automically printing out the closest match, can I show the user a list of closest matches in a dialog and ask them to select one?

1

There are 1 answers

2
Gabe Sechan On BEST ANSWER

No, you can't change the behavior of the default keyboard like that. There's no API to do so, to override what a single key does. If you want this you need to interact with the speech to text API yourself, without using the keyboard. This is built into Android (assuming the phone has a voice provider downloaded), and the API allows the voice provider to return multiple possible results. Whether a given voice provider does or not (remember the user and OEM can install any speech to text provider they want) is something you'd have to experiment and find out.