How to Use Text to Speech on Android

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use Text to Speech on Android
Android applications use Text-To-Speech for a variety of purposes. ( Images)

The Text-To-Speech functionality available on the Android platform allows developers to create applications which can speak text aloud. The Text-To-Speech facility can be tailored to different languages and locales, allowing you to create an experience that is targeted to your application users. Implementing Text-To-Speech within your own Android applications is straightforward. The task can be carried out using syntax that is clear to understand.

Skill level:


  1. 1

    Open the Java file for the class you want to use the Text-To-Speech function in. Add the following import statements at the top of your file:

    import android.speech.tts.TextToSpeech;

    import android.speech.tts.TextToSpeech.OnInitListener;

    import java.util.Locale;

    These are required for using Text-To-Speech and for setting the locale you wish to target.

  2. 2

    Alter your Java class to use Text-To-Speech. To use the Text-To-Speech functionality, your class must implement the correct interface. Alter your class declaration as follows:

    public class SpeechClass extends Activity implements OnInitListener

    This example code is an Android Activity class. You should use whichever class name your code already has; the only change you need to make is adding "implements OnInitListener" to the class declaration.

  3. 3

    Create an instance of the "TextToSpeech" object. This is the main object you will need, so add it as an instance variable near the top of your class declaration, before your "onCreate" method:

    private TextToSpeech tts;

    Inside the "onCreate" method for your Activity, instantiate the Text-To-Speech object:

    tts = new TextToSpeech(this, this);

    Your class now has a usable Text-To-Speech object providing access to the resource functionality.

  4. 4

    Implement the "onInit" method. Your Integrated Development Environment (IDE) may have been highlighting the fact that you have not yet implemented the interface specified in the class declaration. To address this, add the following method outline to your class:

    public void onInit(int arg0) {

    //text to speech code here


    This is the method required to use the Text-To-Speech functionality and will contain the code which sets you up to access it.

  5. 5

    Call the Text-To-Speech functions within your application. Inside the "onInit" method, add the following code:


    You can target any locale you like, but this targets U.S. English. Wherever you want your speech to take place in your class, you can do so using the following syntax:

    String speech Text = "Hello";

    tts.speak(speech Text, TextToSpeech.QUEUE_FLUSH, null);

    Save your code and run the application to test it.

Tips and warnings

  • By altering the "speak" method parameters, you can choose to add the spoken text to a queue or to speak immediately as in this example.
  • Users will need the TTS resources on their system for the function to work, but in most cases it is now included within the platform.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.