package com.example.speak01;

import android.support.v7.app.ActionBarActivity;

import java.util.Locale;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{

	private EditText userInputToSpeech;
    private TextToSpeech convertToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userInputToSpeech = (EditText)findViewById(R.id.speech_input);
        Button textToSpeech = (Button)findViewById(R.id.text_to_speech);

        textToSpeech.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String convertTextToSpeech = userInputToSpeech.getText().toString();
                if (convertTextToSpeech.equals("")) {
                    Toast.makeText(MainActivity.this, "The input field must not be empty", Toast.LENGTH_LONG).show();
                    return;
                }
                convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                    @SuppressLint("NewApi")
					@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
                    
                    
                    @Override
                    public void onInit(int status) {
                        if (status != TextToSpeech.ERROR) {
                            convertToSpeech.setLanguage(Locale.US);
                            convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
                        }
                    }
                });
            }
        });
    }
    
    
	
	
}
