Android comes with an inbuilt feature speech to text through which you can provide speech input to your app. With this you can add some of the cool features to your app like adding voice navigation(Helpful when you are targeting disabled people), filling a form with voice input etc.,

In the background how voice input works is, the speech input will be streamed to a server, on the server voice will be converted to text and finally text will be sent back to our app.

If you want to do the other way i.e converting text to speech, follow my previous tutorial Android Text to Speech

speech to text banner

I have created a simple app to demonstrate this tutorial. Below is the screenshot of the app which contains a simple button to invoke speech input and a TextView to display the converted speech text.

android speech to text

So let’s start by creating simple app.

Sample Application

1. Create a new project in Eclipse by going to File β‡’ New β‡’ Android Application Project and give required information.

2. Open strings.xml located under res β‡’ values and add below string values.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Speech To Text</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="speech_prompt">Say something&#8230;</string>
    <string name="speech_not_supported">Sorry! Your device doesn\'t support speech input</string>
    <string name="tap_on_mic">Tap on mic to speak</string>
</resources>

3. Open colors.xml located under res β‡’ values and add below colors. If you don’t see colors.xml, create a new file and add the values.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="bg_gradient_start">#31244e</color>
    <color name="bg_gradient_end">#6b394c</color>
</resources>

4. Now open the layout file for main activity(activity_main.xml) and add below code to create a simple layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_gradient"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtSpeechInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textColor="@color/white"
        android:textSize="26dp"
        android:textStyle="normal" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/btnSpeak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/ico_mic" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/tap_on_mic"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:textStyle="normal" />
    </LinearLayout>

</RelativeLayout>

5. Finally open your MainActivity.java and do the following changes. In simple adding speech input will be done in two steps.

Step 1: Starting RecognizerIntent
First we need to create a RecognizerIntent by setting necessary flags such as
ACTION_RECOGNIZE_SPEECH – Simply takes user’s speech input and returns it to same activity
LANGUAGE_MODEL_FREE_FORM – Considers input in free form English
EXTRA_PROMPT – Text prompt to show to the user when asking them to speak

Step 2: Receiving the speech response
Once the speech input is done we have to catch the response in onActivityResult and take appropriate action needed.

package info.androidhive.speechtotext;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TextView txtSpeechInput;
	private ImageButton btnSpeak;
	private final int REQ_CODE_SPEECH_INPUT = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
		btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

		// hide the action bar
		getActionBar().hide();

		btnSpeak.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				promptSpeechInput();
			}
		});

	}

	/**
	 * Showing google speech input dialog
	 * */
	private void promptSpeechInput() {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
				getString(R.string.speech_prompt));
		try {
			startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
		} catch (ActivityNotFoundException a) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.speech_not_supported),
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * Receiving speech input
	 * */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQ_CODE_SPEECH_INPUT: {
			if (resultCode == RESULT_OK && null != data) {

				ArrayList<String> result = data
						.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				txtSpeechInput.setText(result.get(0));
			}
			break;
		}

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Run the app in a real device. Make sure that the device has good internet connectivity while you are testing.

Enabling Offline Mode

Right now all the devices are not supporting offline speech input. However you can follow this discussion to enable offline speech input for supported devices.

I have downloaded speech input packages on my Nexus 5 and offline speech is working fine.

1. On your device go to Settings -> Language and Input. Click on icon on Google voice input.
2. Under ALL tab select the language you want to download.
3. Once the language package downloaded, you can see it under INSTALLED tab.

android speech to text offline mode
android speech to text offline mode
android speech to text offline mode
Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.
  • Abhishek

    Nice one Ravi

  • Dinesh

    Nice Post πŸ™‚

  • pawan

    Thumbs up πŸ™‚
    Please provide a tutorial for Sending a list of contacts (phone and sim) to server and how to receive the same from server

    • Hi Pawan,

      Thanks for the suggestion. I’ll try to write it on next week. Uploading them to a server is different topic though, I can explain reading part from SIM/Phone.

      • Pawan

        Thank you Ravi.
        I’ve done an application for displaying a list of contacts but i’m unable to upload and download the list of contacts to and from server respectively. It will be helpful for me if you provide a tutorial for the same. Waiting for the coming week . Thanks in advance

  • Ravi sir, Is there any way to change Google’s stock voice recognition UI ??

  • LouisG

    Can i see your bg_gradient.xml file, i don’t really understand how write it..

    • Oops! I missed it. I’ll update the article.

      Here is the code for gradient bg

  • tEi

    Looks awesome, gonna try it. Many thanks (=

  • Rebecca Wu

    Sponsored Review Inquiry

    Hi,

    This is Bennie from EaseUS Software Company, a leading software
    provider for data backup and recovery.

    We’re recently looking for sponsored reviews. And it occurred to me
    that you might be interested in.

    You can keep your authentic thoughts in the review.

    How do you quote for writing one review with two links?

    Giveaway is also available to us if you’re interested in.

    Please contact bennie@easeus.com if you have any suggestions for cooperation.


    Best regards
    Bennie
    Chengdu Yiwo Tech Co., Ltd.
    http://www.easeus.com
    http://www.partition-tool.com
    http://www.todo-backup.com

  • Hi, thanks for your tutorials. From NO-programmer to a beginner, a huge credit goes to your tutorials (actually just after Google and Stackoverflow).
    Sorry for being off-topic, but I am looking for Pagination of TextView for Android (something like e-book reader). Just wanted to know, if you have made any tutorial/demo of that? All the available demo like pageturner etc. are way too complex for me to understand.
    Or, if you can point me to any other site from where I can get the basic idea for making it?
    (will wait for your reply)
    Thanks.
    Regards,
    Dr. Atul Tiwari

  • guy

    i did not understand this step

    ACTION_RECOGNIZE_SPEECH – Simply takes user’s speech input and returns it to same activity
    LANGUAGE_MODEL_FREE_FORM – Considers input in free form English
    EXTRA_PROMPT – Text prompt to show to the user when asking them to speak

    from where i got those .??

    • ali

      i want ico_mic.xml please

    • hafoosa

      from code …
      it is there in code

  • hafoosa

    i do all steps but not work … what is wrong ???

    it say >Sorry! Your device doesn’t support speech input

  • Waseem

    How to implement pdf to speak in android

  • Tirsa Aprillia

    I need help you and I have sent it to you. Thanks before

  • Tirsa Aprillia

    How to make a speech recognition with function start and stop to speak. Please help me, I need the code

  • fuck me too

    Is internet permission not required?

  • fuck me too

    Excellent Tutorial! You rock dude/

  • Aryan

    What can I put in place of LANGUAGE_MODEL_FREE_FORM to change the language of input. Can it be auto recognized?

  • webdevlamp

    very nice post

  • Mayur

    How can i convert japanese into Text. I tried saying few japanese sentences but unable to get it in japanese text. I even downloaded the japanese language pack in phone. Please help me.

  • KASMIN

    hello sir thanks for helping.
    im new in android development im trying to make an app which listen our voice n make to text of it ..
    here i wann know how to make “append code” to acccept full speech and give it as notes..
    thank you

    • KASMIN

      thanks. i got solution
      i used : tv.append(getResult());

  • chlong chlong

    Hi, How I could implement this to your tab fragment? anyhelp would do…

  • Rahul

    Hi Ravi In this application process speech online why network permission is not unabled in manifest file

  • Sitters

    How can I add a confirmation before? such as Did you say “Hello World” (yes or no)?

  • Ena

    hello, how do you destroy it? i mean, i think it’ll have an error that services are leaking or something? i haven’t seen you closing it in onDestroy