0% found this document useful (0 votes)
14 views4 pages

Text To Speech

The document describes how to build a text-to-speech Android application using XML layout files and Java code. It includes XML for the user interface layout with text fields and buttons. The Java code initializes a TextToSpeech object, retrieves input text from an EditText field, and uses the TextToSpeech API to speak the text aloud on button click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Text To Speech

The document describes how to build a text-to-speech Android application using XML layout files and Java code. It includes XML for the user interface layout with text fields and buttons. The Java code initializes a TextToSpeech object, retrieves input text from an EditText field, and uses the TextToSpeech API to speak the text aloud on button click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

“Program To Implement Text To Speech”

activity_main.xml

<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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110sp"
android:layout_marginTop="100sp"
android:text="TextToSpeech"
android:textColor="#009688"
android:textSize="30sp"
android:textStyle="bold"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter text : "
android:textSize="18sp"
android:layout_marginTop="80sp"
android:layout_marginLeft="50sp"
android:textColor="#673AB7"
android:textStyle="bold"/>

<EditText
android:id="@+id/text"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginTop="-40sp"
android:layout_marginLeft="150sp"
android:inputType="textMultiLine"/>

<Button
android:id="@+id/button"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:text="Speak ->"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="70sp"
android:layout_marginLeft="100sp"/>

</LinearLayout>

MainActivity.java

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

TextToSpeech t1;
EditText e1;
Button b;

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

e1 = findViewById(R.id.text);
b = findViewById(R.id.button);

t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {


@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR)
{
t1.setLanguage(Locale.ENGLISH);
}
}
});

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = e1.getText().toString();
t1.setPitch((float)1.0);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD,null);
}
});

}
public void onDestroy()
{
if(t1 != null) {
t1.stop();
t1.shutdown();
}
super.onDestroy();
}
}

You might also like