Smart Phone Android

Text To Speech App

Before starting example using Android SDK, you have to make sure that you have setup your Android development environment properly in your system and you you have knowledge with Eclipse IDE.

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list.


Create blank activity with MainActivity


This Android app. allows you convert your text into voice. Android provides TextToSpeech class for this purpose. In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below:

private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
public void onInit(int status) {

In this listener , you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below:

following are the steps that demonstrates the working procedure of this app:

1.You will use Eclipse IDE to create an Android application and name it as TextToSpeech under a package com.example.texttospeech. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.

2.Modify src/ file to add TextToSpeech code

3.Modify layout XML file res/layout/activity_main.xml

4.Modify res/values/string.xml file.

5.Run the application and choose a running android device and install the application on it and verify the results.

here is the content of src/com.example.texttospeech/…..

package com.example.texttospeech;

import java.util.Locale;
import java.util.Random;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

TextToSpeech ttobj;
private EditText write;
protected void onCreate(Bundle savedInstanceState) {
write = (EditText)findViewById(;
ttobj=new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
public void onPause(){
if(ttobj !=null){
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
public void speakText(View view){
String toSpeak = write.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);


Here is the content of activity_main.xml….




Here is the content of Strings.xml

Hello world!
Text to Speech
Write Text

Here is the content of AndroidManifest.xml:-




here are the screen shots of the running application.


Now just type some text in the field and click on the text to speech button below. A notification would appear and text will be spoken. It is shown in the image below:


Now type something else and repeat the step again with different locale. You will again hear sound. This is shown below:


Leave a Reply