Hi friends. Today I’ll be posting code which you can use in your project, if you want Android text to Speech functionality in your application. And it’s also fun to create such applications where you can make your mobile phone speak out anything you want??. If you get me. Ok so let’s get started.
I’m using Eclipse ADT to develop this app and you can always use Android Studio if you wish. Application is simple. One Textbox and one button to make the device speak. So create a new application project and name it as you wish. Create new Layout file in res>layout folder and name it speakout.xml and edit it as following :
speakout.xml:
<?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" > <TextView android:id="@+id/txtheader" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight=".1" android:gravity="center" android:padding="3dp" android:text="Speak Out!!!" android:textColor="#fff" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/edtTexttoSpeak" android:layout_width="match_parent" android:layout_weight=".5" android:background="#fff" android:textColor="#2c3e50" android:text="Hi there!!!" android:padding="5dp" android:gravity="top|left" android:layout_height="0dp"/> <Button android:id="@+id/btnspeakout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".1" android:background="#e74c3c" android:textColor="#fff" android:text="SPEAK OUT"/> </LinearLayout>
The above design xml contains one textbox to enter text content and a button to speak out string entered in the textbox.
And make a java class (.java file) in your application project and name it SpeakOut.java and edit it as following :
SpeakOut.java:
package com.example.speechtest; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SpeakOut extends Activity implements OnInitListener { private TextToSpeech repeatTTS; Button btnspeakout; EditText edtTexttoSpeak; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.speakout); btnspeakout = (Button) findViewById(R.id.btnspeakout); edtTexttoSpeak = (EditText) findViewById(R.id.edtTexttoSpeak); repeatTTS = new TextToSpeech(this, this); btnspeakout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { repeatTTS.speak(edtTexttoSpeak.getText().toString(), TextToSpeech.QUEUE_FLUSH, null); } }); } @Override public void onInit(int arg0) { // TODO Auto-generated method stub } }
And in your Android manifest file run this activity. (Edit it).
AndroidManisfest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.speechtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SpeakOut" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now you can run the application. If you found this post useful, please do share this post on facebook, google plus or twitter. You can help this site grow by doing so and I will appreciate that. Also you can comment below if you face any issue with the code, I will be helping you. Cheers.