Open In App

How to Convert Text to Speech in Android?

Last Updated : 10 Sep, 2020
Like Article

Text to Speech App converts the text written on the screen to speech like you have written “Hello World” on the screen and when you press the button it will speak “Hello World”. Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it’s also convenient for those who want to be read too. This feature has come out to be a very common and useful feature for the users.

Running Condition of the App

Note: To implement its vice versa that is to convert speech to text please refer to How to Convert Speech to Text in Android?

Steps for Converting Text to Speech in Android

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with activity_main.xml file

Go to the app -> res -> layout -> activity_main.xml section and set the layout for the app. In this file add an EditText to input the text from the user, a Button, so whenever the user clicks on the Button then it’s converted to speech and a TextView to display the GeeksforGeeks text. Below is the complete code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--To add text in the app-->
        android:hint="Enter Any Sentence"
    <!--when you press this button it will 
        convert text into speech-->
        android:text="Click Here"
    <!--To display the name of GeeksForGeeks -->
        android:textSize="36sp" />

Step 3: Working with file

Go to the app -> java -> com.example.GFG(Package Name) -> section. Now join the Button and Edittext to Java code and comments are added inside code to understand the code easily. Below is the complete code for the file.

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
    EditText Text;
    Button btnText;
    TextToSpeech textToSpeech;
    protected void onCreate(Bundle savedInstanceState) {
        Text = findViewById(;
        btnText = findViewById(;
        // create an object textToSpeech and adding features into it
        textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            public void onInit(int i) {
                // if No error is found then only it will run
                    // To Choose language of speech
        // Adding OnClickListener
        btnText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

The user may choose another language as well. For that refer to the below image to see how to do that.

Output: Run on Emulator

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads