Wednesday, September 1, 2010

Tutorial to develop text to speech app on Android

This tutorial will show how to develop simple Text to speech application on
android(tested on htc droid incredible).  It assumes that you have already
managed to develop hello world(or default project ) in Eclipse per

  • Step 1 - Create default android project in Eclipse. Run it to make
sure it works.

  • Step 2- Replace below code in your main(activity class). You may need to change package and class name.(This code is tweak of sdk sample - ie its simplified )


package com.test;//change this
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.test.R; //change this
import java.util.Locale;
import java.util.Random;
public class TestAct extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
private Button mAgainButton;
public void onCreate(Bundle savedInstanceState) {
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
mTts = new TextToSpeech(this,
this //TextToSpeech.OnInitListener
mAgainButton = (Button) findViewById(;
mAgainButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
public void onDestroy() {
// Don't forget to shutdown!
if (mTts != null) {
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
// Greet the user.
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello World",
"This is Text to speech demo by Zahid Shaikh"
int i =0;
private void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[i];
if(i == helloLength) i =0;
TextToSpeech.QUEUE_FLUSH, // Drop allpending entries in the playback queue.
  • Step 3 -   Change res/main/layout.xml (or equivalent)
<linearlayout android:background="#ff00ff" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="">
    <button android:enabled="false" android:id="@+id/again_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:minlines="3" android:text="@string/again" type="submit"> </button>


  • Step 4 - change res/values/string.xml
    <string name="hello">hi world</string>
    <string name="app_name">test</string>
<string name="again">Again</string>
<string name="red">red</string>

<string name="white">#FF00FF</string>

  • Run your code - and press on the again button!!! 


  1. Thanks for this post ... simple and to the point.

  2. This is really an awesome article to implement Text to speech functionality.

  3. sorry, i became fool, its copy cat from

  4. yup... it says right on top that its a tweak of code sample. but they never gave easy steps to run just text to speech.

  5. Hi Dear ,
    Thanks for your this Tutorials which to help me.
    it was good and I appreciat this but there is some error are facing me but can not solve it properly Error are enclosed bellow.

    "mAgainButton = (Button) findViewById(;"

  6. Hi Zahid,

    I have a problem to import android.speech.tts.TextToSpeech. Why is that?
    Hope anyone could tell me why...?

  7. Wow really i like it you post because very good information for android development thanks for sharing post.

    The usage of this hand held speech device at home or office does not seem to be a problem but the real mess is created when cell phone is being used while driving. Taking calls while driving is risky but more risk shows up when one is texting while driving. When people TWD, they take almost 30 seconds to write a single phrase or around which is more than enough to indulge in distracted driving.

  8. Google is one of the most popular search Engine which is taking a Important part In internet marketing field to generate more traffic.
    Android app developers

  9. Its a good knowledgeable resource and your explanation is easy to understood, so i would like to thank for creating this interesting blog.

    Android developers

  10. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

    iPhone App Development

  11. Excellent Blog. I really want to admire the quality of this post. Its really impressive too..
    Top seo forums

  12. You do not have the skills and workforce required for Android Development, you need to get someone else to do it.

  13. Really your content is so informative. so please share some more content ..
    Android training in Noida