The Best Ways to Utilize Voice Input in Android

Pratik Chauhan
3 min readMay 31, 2021
Photo by Soundtrap on Unsplash

We have all used Siri and Google Voice to tell our commands and make something done.

Since most of the apps we use be it be Food apps, Grocery Apps, Finance Apps, Sports apps have search functionality that helps us to narrow down the result and ease our search.

We can take the search one more level up by integrating the voice input aka Speech to text using the system-provided features.

The usage can vary from places like

  1. Searching for places inside the maps search box
  2. Send a message using voice to text while chatting

It will hardly take a minimal amount of time to add but will reap major benefits thereby also improving the user experience.

Let’s learn today how to integrate voice input mic🎙 in Android apps

Step 1:

We are going to call the system’s built-in Speech Recognizer activity to get speech input from users. Use speech input to send messages or perform searches.

For this purpose, we will have to create RecognizerIntent by using Flags

val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US")
return intent

Let’s jump in to understand these intent flags

ACTION_RECOGNIZE_SPEECH — Starts an activity that will prompt the user for speech and send it through a speech recognizer. The results will be returned via activity results.

EXTRA_LANGUAGE_MODEL — Informs the recognizer of which speech model to prefer when performing. The recognizer uses this information to fine-tune the results of our search.

You can make use of Locale.Default() to get the default system language of the application.

val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault())
return intent

EXTRA_LANGUAGE_MODEL — You can use to customize the text which will be showed in the voice dialog box

val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault())
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to Text")
return intent
By Default Text
Custom text using EXTRA_PROMPT flag

Step 2:

We are going to take the speech intent and start it using startActivityResult(). (Using ActivityResultApi)

private val recognitionCustom = registerForActivityResult(RecognitionContract()) { result ->
binding
.tvInput.editText?.setText(result)
}

We will parse the result we will receive in onActivityResultCallback()
and get the output Since there are plenty of ways to get the result i.e spoken text.

override fun parseResult(resultCode: Int, intent: Intent?): String {
val spokenText =
intent?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.get(0) ?: ""
return
spokenText
}

Here is the complete VoiceRecognitionActivity() for better understanding

VoiceActivityRecognition.kt file

Final Output

https://raw.githubusercontent.com/cpratik711/VoiceInputSearch/main/arts/v1.gif

Hooray,🥳 we are done with the implementation. Hope you have learned something new and will try to add this to your project.

Project source code

Conclusion

This article taught you how to implement on-device Voice input search in Android.

Be sure to clap👏🏻 or recommend this article📰 if you found it helpful. It means a lot to me.😇. Thanks for reading!!!

--

--

Pratik Chauhan

Self taught Android developer matching the speed of Android advancements thereby integrating into the apps.