Android: Speech To Text using API

Android has a very cool feature that still many developers dont know. Apps like Any.DO uses speech to text conversion feature quite creatively. In today’s world of Siri, voice commands are of utmost importance. Android natively provides feature of Speech to Text so why not to use it in our app!

I will show you how to use Android’s Speech to Text API in an application.

Let’s make our demo application.

Demo App

The App will be very simple. It will have a button with Mic symbol. On click of which we trigger Android’s Speech to Text Intent which shows a dialog to take speech input. The speech input is then converted into text. The text is then displayed in a text view.

Step 1: Create Basic Android Project in Eclipse

Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as SpeechToTextDemo and select Android Runtime 2.1 or sdk 7. I have given package name

Once you are done with above steps, you will have a basic hello world Android App.

Step 2: Change the Layout

For our demo, we need simple layout. Just one Image Button to trigger Speech to Text API and one TextView to display result text that is converted from speech.

Open layout/main.xml in your android project and replace its content with following:

File: res/layout/main.xml

<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:src="@android:drawable/ic_btn_speak_now" />

        android:textAppearance="?android:attr/textAppearanceLarge" />


The UI is very simply. One LinearLayout to organize the button and text view. Note the id for button: btnSpeak and text view: txtText which we will use in our Java code.

Step 3: Android Java Code to trigger Speech to Text API

Open SpeechToTextDemoActivity class and replace the code with following.



import java.util.ArrayList;

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 {

	protected static final int RESULT_SPEECH = 1;

	private ImageButton btnSpeak;
	private TextView txtText;

	public void onCreate(Bundle savedInstanceState) {

		txtText = (TextView) findViewById(;

		btnSpeak = (ImageButton) findViewById(;

		btnSpeak.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent intent = new Intent(

				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

				try {
					startActivityForResult(intent, RESULT_SPEECH);
				} catch (ActivityNotFoundException a) {
					Toast t = Toast.makeText(getApplicationContext(),
							"Opps! Your device doesn't support Speech to Text",


	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(, menu);
		return true;

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

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

				ArrayList<String> text = data



The heart of Speech to text Android API is package android.speech and specifically class android.speech.RecognizerIntent. Basically we trigger an Intent (android.speech.RecognizerIntent) which shows dialog box to recognize speech input. This Activity then converts the speech into text and send backs the result to our calling Activity. When we invoke android.speech.RecognizerIntent intent, we must use startActivityForResult() as we must listen back for result text.

Note how in above code we crate intent android.speech.RecognizerIntent and trigger it. Also we add one extra parameter using .putExtra() method. When invoking RecognizerIntent, we must provide extra RecognizerIntent.EXTRA_LANGUAGE_MODE. Here we are setting its value to en-US.

Since we triggered the RecognizerIntent via startActivityForResult(), we override method onActivityResult(int requestCode, int resultCode, Intent data) to handle the result data. The
RecognizerIntent will convert the speech input to text and send back the result as ArraList with key RecognizerIntent.EXTRA_RESULTS. Generally this list should be ordered in descending order of speech recognizer confidence. Only present when RESULT_OK is returned in an activity result. We just set the text that we got in result in text view txtText using txtText.setText().

One thing worth noting here is how to handle devices/android version that doesn’t support speech to text API. In such case, exception ActivityNotFoundException will be thrown when we try to start activity. In above example, we have catched this exception and displayed a message “Opps! Your device doesn’t support Speech to Text” using Toast.

Screen shots of Android App

And that’s all! Just execute the app in Android emulator or real device and see following output.





Download Source Code (350 KB)


RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH Documentation

Get our Articles via Email. Enter your email address.

You may also like...


  1. Sid says:

    This part is not right:
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, “en-US”);

    Should be:
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, “en-US”);


  2. Eman Hassan says:

    I am trying to change the language of the recognition from English to Arabic and I simply did this

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, “ar-QA”); // Qatari Arabic

    But I get the text in English which is fine with me but interpreted wrong

    for example, I say “Yameen = right” >> I get the text as “Yeah mean” :)

    any suggestions on how I might be able to change the language of the recognition and the text?!

    another question, why do I have the button to activate the recognition, can’t I simply keep it listening the min I run the app?

    Thank you so much in advance for helping me understand :)

    • SARA says:

      use this

      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
  3. Ajay Prasad says:

    Hi Viral Patel thanks for such a nice tutorial this program working perfect but i have some question regarding this post, this program recognize only alphabets not numeric or number if i say 100 it will print hundred, actually i want a result if i say 100 then it will show numeric value not a alphabets, can you help me out on this ?????

    • facing the same issue

    • krishna says:

      i am also facing the same problem. did u get any solution (or) work around for this ?

      • Anouar says:

        you guys got the rest as a text, so go ahead and parse the text and replace “hundred” by “100”. simple :)

        text.get(0).replace(“hundred”, “100”)

    • Anouar says:

      if you say “one hundred” it will display “100”. However, if you say “hundred” it will display “hundred”.

  4. pratap says:

    Hello viral patel,

    Thanks for sample application.

    It is working good. but in some devices it is working fine with out internet connection.
    but in some devices , it needs internet connection.

    Is there any other option to make speech to text in offline mode with out internet.


  5. abhijit says:

    Is it possible to run the recognizer intent in the background and it is possible to convert a audio file to text by using this API in android.

  6. Yash Gupta says:

    Hey i want to know can i manually give input to the api . Like a recording to the api to convert it to text ??

  7. Krunal says:

    Great and easy example, can you please give me some suggestions my problem is i want to use the same functionality in a simple java class (non-activity class). how can i achieve this .?

  8. purushoth says:

    hi viral,i’m purushoth,i seen your project it’s fine k k ,i tried in my system this was not run properly if i click the button it’s record my speech it shows only the button(its first screen shot only displayed} help me…

  9. Rakesh says:

    What is the language tag for hindi-India?

  10. shabaz says:

    i download the source code and running it on sdk vertiual andriod device by it giving me error that (opp’s your device do not support speech to text ) what i have to do now

    • hector says:

      maybe you have to cut and paste!

    • Thuannguyen says:

      You must run the Android Project in real device (smartphone). It isn’t run in vitural device because vitural device not supported

  11. hector says:

    das ist bundaba!, excellent, muy bacano—- felicitaciones

    • Julian says:

      Es heißt wunderbar. Aber egal…

  12. Anil rai says:

    sir, this speech to text demo is not working in w-ifi.plz send me the code so it work in w-ifi.
    I am still waiting for your answer.

  13. HAR says:

    vertiual andriod device display an error …opp’s your device do not support speech to text …..any one can help me

    • Luckas says:

      Speech recognition is not supported by it, you have to test on some Android device with this functionality and this API.

  14. bery says:

    please,please who can answer my question:when i change the language of the recognition from English to Arabic i use(ar_SA) it is done but without any open,vibrio,Fracture and serenity on the arabic letters
    ,so what should i do to get the open,vibrio,Fracture and serenity on the arabic letters?
    Thank you so much in advance for helping me understand

  15. kalthum says:

    please help me soon:i want that when i read quran karim the program write it with full arabic language i mean with open,vibrio,Fracture , serenity and others on the arabic letters ,i change
    en-US to ar_SA its done but but without any open,vibrio,Fracture , serenity and others on the arabic letters ,so what should i do,please?or whats the type of english language that i can use to write every letter when i said in arabic language then it writes in english letter for example when i say “بِسْمِ الله” it will write “bismilah”.
    please,please answer me

  16. mark says:

    In my device, this application is not running without an Internet connection. What’s wrong?

  17. sangeeta says:

    hey can anybody mail me code for intenet class

  18. krishna kanth says: cannot be resolved error plz help me guys..!!!

    • vinay says:

      Clean your project. If you still see the error then make sure you are importing the correct R class.
      android.R is the OS provided class while yourpackagename.R will be your class generated on build.

  19. getinet says:

    I want to change the language of the recognition from English to Amharic language.

    any suggestions on how I might be able to change the language of the recognition and the text?!

  20. stephen s d says:

    hello sir, i am doing a project on voice recognization i,e text to voice and voice to text, sir i got text to voice and i wrote the code for voice to text, but in emulator its coming that this device doesn’t support this application, so what to do sir please suggest me and also which supporting files and are needed to run this application, reply me as fast as possible sir please.

  21. Pothiraj says:

    Nice coding.. 100 % working

  22. Es says:

    i want the opposite tts arabic – text arabic to speech – have you solution

  23. Luis says:

    Hi im having some problems , when im creating the layout this problem appear:
    @string/speak: No source found that matches the given name.

  24. krishna says:

    this source code is very understandable.thank you

  25. Aravind says:


    Can anybody confirm me that this code works in offline mode…. for me its working fine in online but when iam trying in offline mode.. it does’t work….. when i went through the code there is no option indicating that internet is required?… so i think this should work in offline….pls suggest ….
    any references which work in offline….


  26. Izy says:

    Hello sir
    thanks for the tutorial…I encounter a problem where when I speak to the device then it load of something..after that the textview area doesn’t show up..any idea why??
    im using samsung galaxy mini
    looking forward for your respond..thank you :)

  27. shruthi says:

    THank u

  28. sumit yadav says:

    Thanks Viral. Its Nice Tutorial. It works perfectly on real android device. Good Luck.

  29. Anthony says:

    Will Android Speech take pre-recorded audio?

  30. praveen says:

    i want speech to text conversion in java please help me

  31. praveen says:

    how do i change my pacakage name…

  32. madhu says:

    thanq …u helped me

  33. Ravi Teja M says:

    While i m trying this app in emulator it is showing the toast message “your device doesn’t support speech to text”. May i know the reason?

  34. Dhruvika says:

    hello sir
    i am Dhruvika.i am mca final year 5 th sem dessertation subject .my dessertation defination is speech to text in gujarti for android mobile.i have no idea how to add gujrati voice typing languge in mobile.

  35. pratik says:


  36. Kasalwe says:

    Perfect. Tried it on my Note 3 and it is just perfect, and simple. Thanks

  37. abhishek says:

    android:src=”@android:drawable/ic_btn_speak_now”…….THIS LINE IS SHOW THE FOLLOWING ERROR IN ECLIPSE…..”@android:drawable/ic_btn_speak_now requires API level 3 (current min is 1)”…..HELP OUT ASAP

  38. Glauber says:

    Cara muito bom, simples e ótimo exemplo!!!!

  39. dan rabino says:

    Hello sir,

    How do you use Natural languange Processing in this program. thank you. urgent badly needed.

  40. vijaya says:

    Hello sir
    I am beginner of android.I have one doubt my application contain Telugu song lyrics files are stored in res/xml/abc,xml like this how to read the files when i reading it display like boxes how to read Telugu in my application and also how to place xml folder in array list folder contain 777 files song lyrics how to read it please help me.

  41. CHUN LI says:

    hey can i convert the speech from english to text in another language,,,,what changings do i ahve to made,,,,PLZ REPLY….

  42. Shobin says:

    My app does not open it opens some other app.please help me…

  43. Shobin says:

    Is it requires internet?

    • yes, but if you download that language library then it will also work offline.

  44. kanagalingam says:

    hi guys,
    i just want to save the speech recognition o/p (i.e., text) in a separate text file like notepad.. How can i do it??? Any suggestions would be greatly appreciated!!!

  45. Fika says:

    I try it in my smartphone, not virtual. But also show ‘your device doesn’t support speech to text’, please help me

Leave a Reply

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