Android Speech to Text Tutorial

You might have already used this feature. We can use voice commands search on google. This feature is inbuilt in Android, and if you want you can use this feature to get voice input in your application as well. And this post will explain to you how to use android speech to text feature in your application.

Now let’s see how we use voice input using Android Speech to Text.

Android Speech to Text Tutorial

Creating a new Project

  • We will start by creating a new Android Studio Project. Here I have created a project named SpeechToText.
  • Now let’s begin with the UI design.

User Interface Design

  • We will try to create a beautiful UI as shown in the below image.
android text to speech app
Android Speech to Text App
  • First, we will define the colors. And to do this go inside, res->values->colors.xml.

  • Change the style to NoActionBar as well (res->values->styles.xml).

  • As you see in the above image, we have a circular button with an audio recorder icon. You can use any image for the icon. But the tricky thing here is we are using both a custom background and a picture. So for this here we will create two more drawable XML files. One is for the default state of the button, and one is for the active state.
  • So first create an XML file named round_default.xml inside the drawable folder.

  • Now, create one more file named round_active.xml inside the drawable folder.

  • Remember, here we are using icon_record which is already in my project’s drawable folder. In your case, you need to paste an image for this inside your drawable folder.
  • Now for the button background, we will create one more drawable resource file inside the same directory. Create a file named button_background.xml and write the following code.

  • Now finally we will come inside activity_main.xml to create our User Interface. Open activity_main.xml and write the following code.

  • After writing the above-given code in your activity_main.xml you will see the following design.

android text to speech

Adding Permission

  • For this application, we need RECORD_AUDIO permission. So, lets first define it in the AndroidManifest.xml.

Checking Permission on Runtime

  • You might already be knowing that after Android Marshmallow we have to ask permission at runtime. So we need to check the permission on runtime if the device running our app is on the android marshmallow or ahead.
  • To do this come inside MainActivity.java and write the following method.

  • Here we are checking if the device is running android marshmallow or ahead. If this condition is true again, we are testing the permission granted or not. If the permission is not granted, we are opening the settings from where the user can allow the permission.
  • So, if the permission is not granted user will see the settings activity, from where the user can allow the permission needed. If the permission is already granted user will directly see our application screen.
  • To achieve our goal that the user should not see the app screen if the permission is not granted, we will first call the above method inside onCreate().

  • Now when you first run the application, you will see the following screen.
android speech to text settings
Android Speech to Text – Settings Screen
  • From this screen, you can allow the RECORD_AUDIO permission. Just click on Permissions and enable the RECORD_AUDIO permission.

Adding Touch Listener to Button

The user has to keep the button pressed when he wants to speak to get the text. So when the button is in the pressed state, it will get the speech and start listening, and after removing the finger from the button, it will stop listening.
To achieve this thing we need an OnTouchListener in our Button.

  • First we will define the EditText.  Inside onCreate() just after the line where we are calling the checkPermission() add the following line.

  • Come inside onCreate() and write the following code after defining the EditText.

  • Now if you run the application, you will see listening after tapping the record button.
android speech to text
Android Speech to Text
  • Now our task is to listen to the speech after this.

Creating a SpeechRecognizer

  • Just after the line where you defined the EditText write the following line.

  • We also need an Intent to listen to the speech. So After the above line write the following code.

Using the RecognitionListener to Listen

  • After defining the Intent. Write the following code.

  • Now you need to modify the OnTouchListener as below.

The Complete Final Code

  • After doing everything as we discussed above, we have our MainActivity.java as shown below.

  • Now you can try running your application.

But remember we need voice data in the device where we are testing the application. You can check the below image to know how to download voice data.

downloading voice data

  • Now you can test the application.
android text to speech app
Android Speech to Text App

Android Speech to Text App Source Code

Still having troubles creating this application? Don’t worry I’ve got the source code for you.
Unlock the link given below to get the source code.

So that’s all for this Android Speech to Text Tutorial. If you have any question regarding this Android Speech to Text tutorial, then let’s discuss it in comments.
If you found this post helpful, then please SHARE it with your friends. Thank You đŸ™‚

9 thoughts on “Android Speech to Text Tutorial”

  1. You are doing great stuff Belal. Your content is one the most original that I find on the net.

    Keep up the good work buddy.

    I keep coming back to your page to learn more and more.

    Reply
  2. I have an error when i run the app, it doesn’t run, it immediatly closes and say the app has stopped.
    Please help me i really need this to work.

    Reply
  3. hello Sir
    your example is great i just needed the same thing but there is more i wanted in it. i want to save audio file in user voice in database and in next layout i want that voice to compare with user voice. Can you please tell me how to do that. thanks your tutorials always helped me in my projects.

    Reply
  4. Hello sir, Thank you for this tutorial. I am getting an error as “bind to recognition service failed”. Can you please tell me solution for it. I have searched over the Internet but I didn’t find any solution.
    Please reply as soon as possible.

    Reply
  5. is this code work on internet or its work without internet . when my internet is on then it work perfectly and when internet is of it not work why help me

    Reply

Leave a Comment