Convert Text to Speech
Convert Text to Speech
1
7) Add PlainText(EditText) component & change the following
properties in XML Code:
• Text: “”
• Hint: “Enter the text to be converted”
• id: “@+id/editText”
8) Add Button component & change the following properties in
XML Code:
• Name: Convert
• onClick: convert
2
XML-CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
3
<TextView
android:id="@+id/textView"
android:layout_width="335dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="21dp"
android:layout_marginBottom="486dp"
android:text="Text2Speech" android:textSize="30sp" />
4
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true“
android:layout_marginEnd="142dp"
android:layout_marginBottom="377dp"
android:ems="10"
android:hint="Enter text here"
android:inputType="textPersonName" />
5
<Button
android:id="@+id/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="196dp"
android:layout_marginBottom="236dp"
android:onClick="convert"
android:background="#6CEC71"
android:text="CONVERT" />
</RelativeLayout>
6
JAVA-CODE:
package com.example.texttospeechapplication;
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;
7
public class MainActivity extends AppCompatActivity
{
EditText e1;
TextToSpeech t1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
8
e1=findViewById(R.id.editText);
t1=new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
9
if(status!=TextToSpeech.ERROR)
{
t1.setLanguage(Locale.UK);
}
}
});
}
public void convert(View V)
{
String tospeak=e1.getText().toString();
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH,null);
}
} 10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35