0% found this document useful (0 votes)
9 views2 pages

Texttospeech

This document describes a simple text-to-speech Android application that allows a user to enter text into an EditText field and have it converted to audio output when a "Convert Text to Speech" button is pressed. The application layout and code are provided, with the layout containing the EditText and button, and the code initializing TextToSpeech functionality and calling it to speak the input text on button click.

Uploaded by

Shashti D B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Texttospeech

This document describes a simple text-to-speech Android application that allows a user to enter text into an EditText field and have it converted to audio output when a "Convert Text to Speech" button is pressed. The application layout and code are provided, with the layout containing the EditText and button, and the code initializing TextToSpeech functionality and calling it to speak the input text on button click.

Uploaded by

Shashti D B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program-7

Develop a simple application with one EditText so that the user can write some text in it. Create a
button called “Convert Text to Speech” that converts the user input text into voice.
Activitymain.xml
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="59dp"
android:layout_marginRight="59dp"
android:layout_marginBottom="649dp"
android:text="Text2SpeechApp"
android:textSize="50dp" />

<EditText
android:id="@+id/editText"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="101dp"
android:layout_marginRight="101dp"
android:layout_marginBottom="514dp"
android:ems="30"
android:hint="Enter the text to be converted"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="162dp"
android:onClick="convert"
android:layout_marginRight="162dp"
android:layout_marginBottom="329dp"
android:text="Convert" />
</RelativeLayout>
Mainactivity.java
package com.shubham.texttospeech;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
TextToSpeech t1;
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = findViewById(R.id.editText);
t1 = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status!=TextToSpeech.ERROR){
t1.setLanguage(Locale.UK);
}
}
});
}
public void convert(View view){
String tospeak = e1.getText().toString();
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH,null);
}
}

You might also like