Hello I am a beginner for android development , The requirement in my app is when user clicks on EditText
the hint Text should also should be converted from Text to Speech using TTS . But i am not getting how to do it,I have tried in EditText's
Onclicklistner
its not working .
I have taken basic code from google.
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:gravity="center_horizontal"
android:text="Android Text to Speech (TTS) Demo"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"/>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text_style"
android:hint="Enter the text here"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp"
android:elevation="0dp"
android:paddingHorizontal="30dp"
android:text="Click to convert text to speech" />
</LinearLayout>
JAVA CODE
public class MainActivity extends AppCompatActivity
{
private TextToSpeech textToSpeech;
private Button btn;
private EditText editText;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
editText = (EditText) findViewById(R.id.et);
textToSpeech = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
int ttsLang = textToSpeech.setLanguage(Locale.UK);
if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang ==
TextToSpeech.LANG_NOT_SUPPORTED)
{
Log.e("TTS", "The Language is not supported!");
}
else
{
Log.i("TTS", "Language Supported.");
}
Log.i("TTS", "Initialization success.");
}
else
{
Toast.makeText(getApplicationContext(), "TTS Initialization
failed!", Toast.LENGTH_SHORT).show();
}
}
});
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
String data = editText.getText().toString();
int speechStatus = textToSpeech.speak(data,
TextToSpeech.QUEUE_FLUSH,null);
if (speechStatus == TextToSpeech.ERROR)
{
Log.e("TTS", "Error in converting Text to Speech!");
}
}
});
// btn.setOnClickListener(new View.OnClickListener()
// {
// @Override
// public void onClick(View arg0)
// {
// String data = editText.getText().toString();
// // Log.i("TTS", "button clicked: " + data);
// int speechStatus = textToSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);
// if (speechStatus == TextToSpeech.ERROR)
// {
// Log.e("TTS", "Error in converting Text to Speech!");
// }
// }
// });
}
@Override
public void onDestroy()
{
super.onDestroy();
if (textToSpeech != null)
{
textToSpeech.stop();
textToSpeech.shutdown();
}
}
}