title: Text to speech android code
body: <p>I would like to ask about the correction of my problem. I saw an article that teaches how to use text to speech android code but I followed the code and my AVD does not seems to respond. So I went for another code and type it in, the avd does not seems to respond too. Can someone help me to check the error?</p>
  
  <pre><code>package net.learn2develop.tts;
  
  import java.util.Locale;
  import java.util.Random;
  
  import android.support.v7.app.ActionBarActivity;
  import android.content.Intent;
  import android.app.Activity;
  import android.os.Bundle;
  import android.speech.tts.TextToSpeech;
  import android.speech.tts.TextToSpeech.OnInitListener;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  public class MainActivity extends ActionBarActivity implements OnInitListener{
  
  private int MY_DATA_CHECK_CODE=0;
  private TextToSpeech tts;
  private EditText inputText;
  private Button speakButton;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      inputText=(EditText)findViewById(R.id.editText1);
      tts=new TextToSpeech(getApplicationContext(),
      new TextToSpeech.OnInitListener(){
          @Override
          public void onInit(int status){
              if(status!=TextToSpeech.ERROR){
                  tts.setLanguage(Locale.UK);}
              }
          });
      }
  
  @Override
  public void onPause(){
      if(tts!=null){
          tts.stop();
          tts.shutdown();
      }
      super.onPause();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  public void speakText(View view){
      String toSpeak=inputText.getText().toString();
      Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
      tts.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);
  }
  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onInit(int status) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>main.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="net.learn2develop.tts.MainActivity" &gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true"
      android:layout_marginBottom="188dp"
      android:layout_marginRight="67dp"
      android:onClick="speakText"
      android:text="@string/text1"/&gt;
  
  &lt;EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/button1"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="81dp"
      android:ems="10"&gt;
      &lt;requestFocus/&gt;
  &lt;/EditText&gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="20dp"
      android:text="@string/write"
      android:textAppearance="?android:attr/textAppearanceLarge"/&gt;
  </code></pre>
  
  <p></p>