Android TextToSpeech Example

nilan avtar

Written by:

Nilanchala,  11 min read,  updated on September 17, 2023

Android operating system provides developers some of the cool API’s such as Text to speech API. converting Text-To-Speech (TTS) or also known as “speech synthesis”. Text-To-Speech enables your Android device to speak text of different languages.

  1. It is introduced from android 1.6, API Level 4. All of the TTS API’s are available in android.speech.tts.TextToSpeech class.
  2. TTS supports various languages like English, French, German, Italian and Spanish, etc. and some additional languages depending on the region.
  3. You have to pre-configure the Text-To-Speech engine with which language to speak. The initial language configuration is important to initialize the specific voice dictionary.

Following section of this tutorial will teach you the steps required to implement TTS in your Android application.

TextToSpeech Example

Before you go ahead implementing the Text to Speech, you must note the following points

  1. A TextToSpeech instance can only be used to synthesize text once it has completed its initialization.
  2. The constructor of TextToSpeech class takes TextToSpeech.OnInitListener parameter. You must implement the TextToSpeech.OnInitListener to be notified, when the TTS is initialized.
  3. Call speak() method on TextToSpeech instance for making Text to Speech to work.
  4. The speak() method accepts three parameter, a String that accepts text, an integer queue mode and Bundle. The queue mode can be either QUEUE_FLUSH or QUEUE_ADD.

In this example, we have an simple activity with an EditText and button, that allows user to enter text for converting into Speech. A screenshot of output is shown below

Android TextToSpeech Example

Define Activity Layout

As described above, for the sake of simplicity we will be creating the simple layout with an EditText and a Button.

<LinearLayout xmlns:android=""
    android:padding="20dp" >

        android:hint="Enter your command"
        android:padding="10dp" >
        <requestFocus />

        android:text="Speak Now" />


TextToSpeech Manager

In this example, we will be crating a generic class named TTSManager, which will encapsulate all of the logic to implement TextToSpeech in Android. You can reuse the class and use it anywhere you like.

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.Locale;

 * Created by Nilanchala
public class TTSManager {

    private TextToSpeech mTts = null;
    private boolean isLoaded = false;

    public void init(Context context) {
        try {
            mTts = new TextToSpeech(context, onInitListener);
        } catch (Exception e) {

    private TextToSpeech.OnInitListener onInitListener = new TextToSpeech.OnInitListener() {
        public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTts.setLanguage(Locale.US);
                    isLoaded = true;

                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("error", "This Language is not supported");
                } else {
                    Log.e("error", "Initialization Failed!");

    public void shutDown() {

    public void addQueue(String text) {
        if (isLoaded)
            mTts.speak(text, TextToSpeech.QUEUE_ADD, null);
            Log.e("error", "TTS Not Initialized");

    public void initQueue(String text) {

        if (isLoaded)
            mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            Log.e("error", "TTS Not Initialized");

How it works

  1. To make the TTS engine to work, the basic configuration is the language that TTS will initialize. You can set the language just by calling setLanguage(locale) method.
  2. Before we call setLanguage(), you need to check if the specified locale is supported. To query whether a specific Locale is supported, you can use isLanguageAvailable(), which returns the level of support for the given Locale.
  3. The TTS engine manages a global queue of all the entries to synthesize, which are also known as “utterances”.
  4. Each TextToSpeech instance can manage its own queue in order to control which utterance will interrupt the current one and which one is simply queued.

Making your app to speak

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private Button speakNowButton;
    private EditText editText;
    TTSManager ttsManager = null;

    protected void onCreate(Bundle savedInstanceState) {

        ttsManager = new TTSManager();

        editText = (EditText) findViewById(;
        speakNowButton = (Button) findViewById(;
        speakNowButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                String text = editText.getText().toString();

     * Releases the resources used by the TextToSpeech engine.
    public void onDestroy() {

Queue multiple texts

If you have multiple text to be queued for TTS engine, you may do it by calling speak() method with TextToSpeech.QUEUE_ADD flag.

String myText1 = "Hi";
String myText2 = "Hello";
//adding it to queue

How it works

  1. Here in this example, the call to the first speak() method would interrupt whatever was currently being synthesized and the queue is flushed and the new utterance is queued, which places it at the head of the queue.
  2. On the second call to speak() method, the utterance is queued and will be played after myText1 has completed.