How to change text to speech voice in android

Hello friends, in this article we are going to learn how to use text to speech voice in android studio step by step.

In this example, we are going with the new project, if you want you can use this code in your existing project. So let’s start the new project.

Use text to speech in Android Studio

Step 1. Create a new project with empty activity, then use the below code inside the.XML file. In this example we are using LinearLayout with orientation vertical, then one text view, one edit text, and one button.

.XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="20dp"
 android:gravity="center"
 tools:context=".MainActivity">

<TextView
 android:id="@+id/ttxt"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Enter your text below"
 android:textAlignment="center"
 android:textSize="20dp"
 android:gravity="center_horizontal"></TextView>

<EditText
 android:id="@+id/txt"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textSize="20dp"
 android:layout_marginTop="10dp"></EditText>

<Button
 android:id="@+id/btn_c"
 android:text="Click here"
 android:textSize="50dp"
 android:layout_marginTop="30dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center"></Button>

</LinearLayout>

Step 2. Add below code inside your .java file.

.Java file code:

package com.ocpgroup.text_to_speach_demo;

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 android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

TextView textView;
EditText editText;
Button button;

TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView=findViewById(R.id.ttxt);
button=findViewById(R.id.btn_c);
editText=findViewById(R.id.txt);

textToSpeech=new TextToSpeech(getApplicationContext()
, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
//SELECT YOUR LOCAL LANGUAGE
 int lang = textToSpeech.setLanguage(Locale.ENGLISH);

}
}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Get data from your edit text view
 String s = editText.getText().toString();

//Below code will convert Your text into speech
 int speech = textToSpeech.speak(s,TextToSpeech.QUEUE_FLUSH,null);
}
});
}
}

Your text to speech is ready now, run your application and use it.

You Read to Like: How to Use GIF file in Android Studio

Connect with me on:

Instagram: https://www.instagram.com/shripaldba
Linkedin: 
https://www.linkedin.com/in/shripal-singh
Twitter: 
https://twitter.com/ocptechnology
Facebook: 
https://www.facebook.com/ocptechnology
YouTube:
 https://www.youtube.com/ocptechnology

Share this
Share
Share