How to Convert Text to Speech in Android?
Last Updated :
10 Sep, 2020
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.
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
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
android:layout_margin = "30dp"
tools:context = ".MainActivity" >
< 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" />
< Button
android:layout_width = "wrap_content"
android:id = "@+id/btnText"
android:layout_height = "wrap_content"
android:text = "Click Here"
android:layout_gravity = "center" />
< 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);
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit( int i) {
if (i!=TextToSpeech.ERROR){
textToSpeech.setLanguage(Locale.UK);
}
}
});
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...