title: how to check whether tts has finished speaking?
body: <p>I've created a Text To Speech android application. I want to change button text from <strong>speak</strong> to <strong>stop</strong> and then again to <strong>speak</strong> when it finishes speaking.</p>
  
  <p>It has two buttons- <strong>clear all</strong> and <strong>speak</strong>. <strong>speak</strong> should change to <strong>stop</strong> on click and again to <strong>speak</strong> when speaking is done.</p>
  
  <p><a href="https://i.stack.imgur.com/N4vyU.png" rel="nofollow noreferrer">Activity screenshot (UI)</a></p>
  
  <p>I searched a lot on StackOverflow as well as the web including their official docs but couldn't find a solution to fit into my program.</p>
  
  <p>Please help me I'm new to android.
  ThankYou!</p>
  
  <pre><code>  //text to speech
  
      t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
          @Override
          public void onInit(int status) {
  
              if (status != TextToSpeech.ERROR)
                  t1.setLanguage(Locale.UK);
  
                  }
      });
  
  
  
  
      //onClick
      b1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String toSpeak = ed1.getText().toString();
              if(flag==0) { //flag variable to check state and to change button text
                  if(toSpeak=="")
                  {
                      Toast.makeText(getApplicationContext(), "Empty, enter something!", Toast.LENGTH_SHORT).show();
                  }
                  else {
                      Toast.makeText(getApplicationContext(), "Speaking", Toast.LENGTH_SHORT).show();
                      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                      flag = 1;
                      b1.setText("Stop");
                      flag = 1;
                  }
              }
              else {
  
                  flag=0;
                  String te=ed1.getText().toString();
                  ed1.setText("");
                  toSpeak = ed1.getText().toString();
                  ed1.setText(te);
                  t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                  b1.setText("Play");
                  Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
  
  
  
  
              }
  
          }
      });
  
  
  //clear all button. clears the text field and changes text on the button1 to play
          b2.setOnClickListener(new View.OnClickListener() {
              @Override
          public void onClick(View view) {
  
                  ed1.setText("");
                  b1.setText("Play");flag=0;
          }
          });
  </code></pre>