How to Convert Text to Speech in Android?

Here is source code of the Program to Demonstrate Text to Speech in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.texttospeech;
import java.util.Locale;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements
        TextToSpeech.OnInitListener {
    private TextToSpeech tts;
    private Button btn;
    private EditText txt;
    public void onCreate(Bundle savedInstanceState) {
        tts = new TextToSpeech(this, this);
        btn = (Button) findViewById(;
        txt = (EditText) findViewById(;
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
    public void onDestroy() {
        if (tts != null) {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
        } else {
            Log.e("TTS", "Initilization Failed!");
    private void speak() {
        String text = txt.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);


Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.