Reading the tiny text on a mobile screen is never a fun experience. That text reading done by your android application is another level of experience. Now, get ready to make a Text to speech application.
Now Open Android Studio and create a project.
After creating the project, you can open the activity_main.xml file in res/layout and type the following code.
Now, its time to code java file. Open Main Activity and type the following code.
Now Open Android Studio and create a project.
After creating the project, you can open the activity_main.xml file in res/layout and type the following code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3498db"
android:weightSum="1"
android:orientation="vertical" >
<EditText
android:id="@+id/text_enter"
android:layout_width="match_parent"
android:layout_weight=".5"
android:background="#fff"
android:textColor="#2c3e50"
android:hint="Type Here!!!"
android:padding="5dp"
android:gravity="top|left"
android:layout_height="0dp"/>
<Button
android:id="@+id/btn_speakout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:background="#e74c3c"
android:textColor="#fff"
android:text="SPEAK OUT"/>
</LinearLayout>
package com.abnhive.tts;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech readTTS;
Button btnSpeak;
EditText textEnter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSpeak = findViewById(R.id.btn_speakout);
textEnter = findViewById(R.id.text_enter);
readTTS = new TextToSpeech(this,this);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readTTS.speak(edtTexttoSpeak.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
}
});
}
@Override
public void onInit(int status) {
}
}
Our app is ready. You can run the app or build the apk file and test on your device. The output looks like this.
Comments
Post a Comment