package com.testotspeech;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class AndroidTestToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
private ArrayList<String> itemsList;
private Spinner spinner;
private String contry_name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
itemsList = new ArrayList<String>();
itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,itemsList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
contry_name=String.valueOf(spinner.getSelectedItem());
System.out.println(contry_name);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.);
I have some problems. First in the spinner before I make any selection when running the application I see all the items like this: [en, en_AU, en_BE ,en_BW…and so on. I want that when i click the arrow down of the spinner I will see all the items in a row one by one and when I select one of them it will be use as LOCALE
So I need to fix this somehow.
Second thing is how do I use the contry_name string when selecting item in the int result = tts.setLanguage(Locale.); ?
Now its: int result = tts.setLanguage(Locale.ENGLISH); but instead ENGLISH I want it to be the selected item from the spinner.
Below snippet will help you.