How to Convert Text to Speech in Android? (original) (raw)

Last Updated : 15 Jul, 2025

**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 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: Android Permission Added

Internet Permission of Android Manifest are need to be added

**Step 3: **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.

activity_main.xml `

<!--To add text in the app-->
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Text"
    android:layout_marginBottom="20dp"
    android:hint="Enter Any Sentence"
    android:gravity="center"
    android:textSize="16dp"/>

<!--when you press this button it will 
    convert text into speech-->
<Button
    android:layout_width="wrap_content"
    android:id="@+id/btnText"
    android:layout_height="wrap_content"
    android:text="Click Here"
    android:layout_gravity="center"/>

<!--To display the name of GeeksForGeeks -->
<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:gravity="center_horizontal"
    android:text="GEEKSFORGEEKS"
    android:textColor="@android:color/holo_green_dark"
    android:textSize="36sp" />

`

**Step 4: Working with MainActivity.java file

Go to the app -> java -> com.example.GFG(Package Name) -> MainActivity.java 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 **MainActivity.java file.

MainActivity.java `

package com.gfg.android_convert_text_speech; import androidx.appcompat.app.AppCompatActivity; 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Text = findViewById(R.id.Text);
    btnText = findViewById(R.id.btnText);

    // create an object textToSpeech and adding features into it
    textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {

            // if No error is found then only it will run
            if(i!=TextToSpeech.ERROR){
                // To Choose language of speech
                textToSpeech.setLanguage(Locale.UK);
            }
        }
    });

    // Adding OnClickListener
    btnText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textToSpeech.speak(Text.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
        }
    });

}

}

`

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