Open In App

How to Convert Text to Speech in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="30dp"
    tools:context=".MainActivity">
    
    <!--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" />
  
</LinearLayout>


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




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



Last Updated : 10 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads