---
title: "Text to Speech in Android"  
description: "In this article I am explaining about Text to Speech in Android"  
author: "Manoj Pandey"  
published: 2015-03-10  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1642/text-to-speech-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Text to Speech in Android

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) is providing a cool feature (from Android 1.6) called [Text to Speech](https://www.mindstick.com/articles/95/text-to-speech-in-csharp-dot-net) (TTS) which speaks the text in different languages. This [tutorial](https://yourviews.mindstick.com/view/81591/semrush-tutorial-and-review-2020-how-to-use-it-to-20x-your-website-traffic) explains how to work with android text to speech or android speech synthesis. In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) i also explained changing the [language](https://yourviews.mindstick.com/view/81607/hindi-language-in-pakistan-is-getting-killed) type, pitch level and speed level.

Android provides Speech [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers) that makes Text to Speech [conversion](https://yourviews.mindstick.com/view/81093/palghar-sadhu-lynching-urban-naxals-and-conversion-lobby-behind-it) very easy. android.speech.tts.TextToSpeach class provides necessary [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) to the trick. At the start we need to implement the TextToSpeech.OnInitListener to do the [initialization](https://www.mindstick.com/interview/608/what-is-the-difference-between-assignment-and-initialization). During initialization we can set the Locale, Audio pitch rate, audio speed. setLanguage() method is used to set the language.

##### In this article I am creating a sample for convert text to speech

##### \

1. Create an android project

2. Add following views in activity_main.xml file

```
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#282828"    android:orientation="vertical" >     <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:padding="15dip"        android:text="Text To Speech"        android:textColor="#0587d9"        android:textSize="26dip"        android:textStyle="bold" />     <Spinner        android:id="@+id/spin"           android:layout_margin="10dip"        android:layout_width="fill_parent"        android:layout_height="52dp"        android:background="@android:drawable/btn_dropdown"        android:focusable="false" />     <EditText        android:id="@+id/txtText"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="10dip"        android:layout_marginTop="20dip"        android:hint="Enter some text to speak"        android:textColor="#ffffff" />     <Button        android:id="@+id/btnSpeak"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="10dip"        android:text="Speak Out" /> </LinearLayout>
```

3. Add following code in MainActivity.class

```
package com.example.androiddemospeech; import java.util.ArrayList;import java.util.Locale; import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView; public class MainActivity extends Activity implements           TextToSpeech.OnInitListener {     /** Called when the activity is first created. */      private TextToSpeech tts;     int spinPosition = 0;     private Button btnSpeak;      private EditText txtText;     Spinner spinner;     ArrayList<String> llist;     ArrayAdapter<String> adapter;      @Override     public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           spinner = (Spinner) findViewById(R.id.spin);           // add item in arraylist           llist = new ArrayList<String>();           llist.add("Select  Language");           llist.add("CANADA");           llist.add("CHINESE");           llist.add("ENGLISH");           llist.add("FRANCE");           llist.add("GERMAN");           llist.add("ITALIAN");           llist.add("JAPAN");           llist.add("UK");           llist.add("US");            // add list in array adapter           adapter = new MyAdapter(getApplicationContext(),               android.R.layout.simple_expandable_list_item_1, llist);            // set adapter in spinner           spinner.setAdapter(adapter);      // TextToSpeech synthesizes speech from text for immediate playback       //or to create a sound file           tts = new TextToSpeech(this, this);            btnSpeak = (Button) findViewById(R.id.btnSpeak);            txtText = (EditText) findViewById(R.id.txtText);            // button on click event           btnSpeak.setOnClickListener(new View.OnClickListener() {                 @Override                public void onClick(View arg0) {                     speakOut();                }            });     }      @Override     public void onDestroy() {           // Don't forget to shutdown!           if (tts != null) {                tts.stop();                tts.shutdown();           }           super.onDestroy();     }      @Override     public void onInit(int status) {           // TODO Auto-generated method stub           int result;           // check item selection from spinner           if (status == TextToSpeech.SUCCESS) {                switch (spinPosition) {                 case 1:                     result = tts.setLanguage(Locale.CANADA);                      break;                case 2:                     result = tts.setLanguage(Locale.CHINESE);                      break;                case 3:                     result = tts.setLanguage(Locale.ENGLISH);                      break;                case 4:                     result = tts.setLanguage(Locale.FRANCE);                      break;                case 5:                     result = tts.setLanguage(Locale.GERMAN);                      break;                case 6:                     result = tts.setLanguage(Locale.ITALIAN);                      break;                case 7:                     result = tts.setLanguage(Locale.JAPAN);                      break;                case 8:                     result = tts.setLanguage(Locale.UK);                      break;                case 9:                     result = tts.setLanguage(Locale.US);                      break;                default:                     result = tts.setLanguage(Locale.ENGLISH);                     break;                }                 // tts.setPitch(5); // set pitch level                 // tts.setSpeechRate(2); // set speech speed rate                 if (result == TextToSpeech.LANG_MISSING_DATA                       || result == TextToSpeech.LANG_NOT_SUPPORTED) {                 } else {                     btnSpeak.setEnabled(true);                     speakOut();                }            } else {            }      }      private void speakOut() {            String text = txtText.getText().toString();            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);     }    // Add custom adapter class for spinner item      public class MyAdapter extends ArrayAdapter<String> {            public MyAdapter(Context context, int textViewResourceId,            ArrayList<String> llist) {                 super(context, textViewResourceId, llist);                 // TODO Auto-generated constructor stub            }            @Override           public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {                 LayoutInflater inflater = getLayoutInflater();                 View spinnerItem = inflater.inflate(                 android.R.layout.simple_spinner_item, null);                 TextView mytext = (TextView) spinnerItem                 .findViewById(android.R.id.text1);                 mytext.setText(llist.get(position));                 mytext.setTextColor(Color.RED);                                // set selected item position                spinPosition = position;                mytext.setTextSize(16);                mytext.setPadding(0, 5, 5, 6);                 return spinnerItem;            }     }}
```

Now run your [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)

![Text to Speech in Android](https://www.mindstick.com/mindstickarticle/10a4211d-f500-47f6-a6a3-d9922d0b65ee/images/eae16f68-0280-4b67-abd7-120ad442fbca.png)

---

Original Source: https://www.mindstick.com/articles/1642/text-to-speech-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
