How can I convert this TOAST message to voice in Android?
Example
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();
How can I convert this TOAST message to voice in Android?
Example
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();
First import the package
import android.speech.tts.TextToSpeech;
Then initialize
private TextToSpeech tts;
tts = new TextToSpeech(this, this);
Finally make a function like this
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Woops. I forgot, you'll also need to define an onInit function
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
In this example I used an onClickListener
to call this function, using a button. Modify it to how you want to call this function when you toast a message.
Just make the String text
as your toast message. In the example above txtText
was an editText. Modify as per your requirement
TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null);