How to convert Text to Speech in your Android App? – Complete source code

This video shows a simple steps to create an App which can convert the Text to Speech. Similar to if you want to have your phone read out something for you. Say for example if you want to have your Android App to read out a string message or some passage or some texts from PDF then it is the best App to create it.

The steps shown are very simple. It just uses TextToSpeech object in the Android’s java code and converts the string entered in the Edit Text widget in the layout. By the click of a button, in the onclick method the TextToSpeech.speak mthod is called to generate the speech for it.

In this video it is also shown how you can select different locale (such as US, UK, Canada) and set the speech rate using the properties of the TextToSpeech object.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

package com.example.mysampletexttospeech;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private EditText editText;
private TextToSpeech textToSpeech;

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

editText = findViewById(R.id.editText);
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
textToSpeech.setLanguage(Locale.US);
textToSpeech.setSpeechRate((float) 2.5);

}
});
}

public void TextToSpeechButton(View view){
textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null,null);
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mysampletexttospeech”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<EditText
android:id=”@+id/editText”
android:layout_width=”360dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”150dp”
android:ems=”10″
android:gravity=”start|top”
android:hint=”@string/your_text_here”
android:inputType=”textMultiLine”
android:autofillHints=”” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”120dp”
android:layout_marginTop=”60dp”
android:onClick=”TextToSpeechButton”
android:text=”@string/text_to_speech_convert” />
</RelativeLayout>

Leave a Reply

%d bloggers like this: