In this tutorial we will create Text To Speech Converter.
1. Create New Android Application Project. Name it as Text To Speech Converter.
2. Open Your activity_main.xml file.
3. Replace your activity_main.xml code with below code.
4 . Add Below Code To your MainActivity.java
MainActivity.java
1. Create New Android Application Project. Name it as Text To Speech Converter.
2. Open Your activity_main.xml file.
3. Replace your activity_main.xml code with below code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Enter Text Below"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:typeface="monospace" />
<EditText
android:id="@+id/EditText1"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:typeface="monospace" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/EditText1"
android:scrollHorizontally="false"
android:text="Click Here To Speek" />
</RelativeLayout>
4 . Add Below Code To your MainActivity.java
MainActivity.java
package
arshad.texttospeechconverter;
import java.util.ArrayList;
import android.net.Uri;
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;
import
android.widget.Toast;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import
android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech tts;
private EditText inputText;
private Button speakButton;
final Context context = this;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (EditText)
findViewById(R.id.EditText1);
speakButton = (Button)
findViewById(R.id.button1);
speakButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String
text = inputText.getText().toString();
if (text!=null &&
text.length()>0) {
Toast.makeText(MainActivity.this, "Saying:
" +
text, Toast.LENGTH_LONG).show();
tts.speak(text,
TextToSpeech.QUEUE_ADD, null);
}
}
});
Intent
intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent,
MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode ==
TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create
the TTS instance
tts = new TextToSpeech(this, this);
}
else {
// missing data,
install it
Intent
installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status ) {
if (status ==
TextToSpeech.SUCCESS) {
Toast.makeText(MainActivity.this,
"Text-To-Speech
engine is initialized", Toast.LENGTH_LONG).show();
}
else if (status ==
TextToSpeech.ERROR) {
Toast.makeText(MainActivity.this,
"Error
occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
}
5. Now Compile And Run Your Project.
6. My Output Screen is as below.
6. My Output Screen is as below.
All The Best : )