package com.samples.api.tts;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TtsActivity extends Activity 
	implements TextToSpeech.OnInitListener{

	private TextToSpeech tts;
	private Button bSpeech;
	private EditText text; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    
	    text = (EditText)findViewById(R.id.text);	     
	    bSpeech = (Button)findViewById(R.id.bSpeech);
	    bSpeech.setOnClickListener(bSpeechClick);
	    bSpeech.setEnabled(false);
	    
	    tts = new TextToSpeech(getApplicationContext(), this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		tts.shutdown();
	}
	
	@Override
	public void onInit(int status) { 
		if (status == TextToSpeech.SUCCESS) {
			bSpeech.setEnabled(true);
		}
	}

	public OnClickListener bSpeechClick = new OnClickListener() {	
		@Override
		public void onClick(View arg0) {
		    tts.speak(text.getText().toString(), 
		    		TextToSpeech.QUEUE_FLUSH, null);
		}
	};
}