Android Speech to Text using Google Api


Android Speech to Text Google Api – hello guys and welcome to another android studio tutorial, in this tutorial article I’m going to show you how to use android speech to text Google API. so basically as you can see on the below image here our activity will have one image view button and one edit text so whenever a user clicks the microphone little icon or a button it will open a white dialog box in which users should start talking to the microphone and then the Google API will recognize those words or sentences and after that, it will set those words into our edit text.

android speech to text

android speech to text

I showed you a few screenshots so you could understand how it works.

Creating Android Speech to text Application:

 To create new project in the android studio simply click the new project button as you can see in the below figure

android speech to text

Then select the empty activity and click on the next button

android speech to text

Then set Application name in my case I select SpeechToText and select java language if you working on java programming and click on the finish button

android speech to text

Android Speech To Text Button:

Make a simple button in your favorite editor in my case I used photoshop for creating a simple mic button and save it into png form

android speech to text

Now copy the designed mic button to your project drawable folder

android speech to text

Android Speech to text XML Code:

so as you can see here I used one image view button and one edit text and I am going to use the onClickListener  event on this button to listen to the user’s words using android speech-to-text Google API

Android Speech to text Java Code:

Code Explanation:

First, I create those two objects for our two UI elements first is ImageView I name it Btnmic and for edittext I name it txtField

then I create a global variable of recognizer and assign a value

in onCreate method, I find the id for those two elements BtnMic is the id of the button and txtField is the id of edit tex field

Then I create onclicklistener for the imageview element and pass onclicklistener as a parameter and in our onclick method we are going to use intent in order to use the android speech-to-text google API so in the intent, I pass RecognizerIntent and I used


to connect the API. next I used speechIntent. putExtra method in the intent so the first parameter is  RecognizerIntent.EXTRA_LANGUAGE_MODEL  and the second parameter is

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM   so if you’re wondering what those two means well the first extra language model informs the recognizer which speech model to prefer and the language model free form use a language model based on free form speech recognition this is a value to use for the extra language model.

 Then I use intent to putExtra extra prompt so RecognizerIntent.EXTRA_PROMPT  and the second parameter will be simple string so this extra prompt is just an optional text prompt to show to the user when actually asking them to speak so you can basically type here anything you want.

Then I use startActivityForResult and pass speech intent as a first parameter and as a second parameter, I pass integer global variable RecognizerResult.

For creating onActivityResult method press ctrl + o and type onActivityResult to find the method so we need to override this method and press the ok button

android speech to text


In onActivityResult I check the requestCode and the resultCode so in the if block I am going to say if requestCode is the same as our code which we passed in our global integer variable and resultCode is result_ok then store those words or text into our array list so just import array list.

Then  I use edit text to set the text from the android speech to text API from our ArrayList and convert it to tostring.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button