This is my code:
package com.testotspeech;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
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;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class AndroidTestToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener, OnItemSelectedListener {
/** 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;
private ArrayAdapter<String> dataAdapter;
private TextView textview;
private Iterator itr;
private String[] t = {"Please Select An Item"};
@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);
spinner.setOnItemSelectedListener(this);
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,t);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(dataAdapter);
textview = (TextView)findViewById(R.id.textView1);
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.ENGLISH);
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!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if (position == 0)
{
}
else
{
}
}
public void onNothingSelected(AdapterView<?> parent) {
textview.setText("");
}
}
EDITED:
What i want to do is when i click on thw spinner it will collapse/open down and i will have for each item and box of it self and it will be in row for example i clicked on the spinner i will see now under it:
Hello
Bye
Daniel
and now if i click on Hello it will put the Hello in the textView1 if i click on the Bye it will put it also in textView1 and so on. But the graphics the designing of the spinner i want it to be that when i click on it it will collapse down and show me the items in a row so i can click and select each item in a single click.
Now what i did is just adding the spinner a text "Please Select An Item" I uploaded image of how i wanted it to be like for example: