title: Clear the contents of an EditText Field once button is pressed
body: <p>I am creating a simple app which automatically triggers a button <code>b</code> once the contents of an <code>EditText</code> field called <code>a</code> contains a key phrase.  I am wanting to clear the contents of the edit text field automatically say after 2 seconds.  Is there a easy way to implement this into my code below?</p>
  <pre><code>public class PocketSphinxActivity extends Activity implements RecognitionListener
  {
  
  private static final String KWS_SEARCH = &quot;wakeup&quot;;
  
  /* Keyword we are looking for to activate menu */
  private static final String KEYPHRASE = &quot;open voice command&quot;;   //adjust this keyphrase!
  
  private SpeechRecognizer recognizer;
  private HashMap&lt;String, Integer&gt; captions;
  
  ListView lv;
  TextView tv;
  EditText a;
  Button b;
  Button c;
  
  Boolean isDone = false;
  
  @Override
  public void onCreate(Bundle state) {
      super.onCreate(state);
  
      // Prepare the data for UI
      captions = new HashMap&lt;String, Integer&gt;();
      captions.put(KWS_SEARCH, R.string.kws_caption);
      setContentView(R.layout.main);
      ((TextView) findViewById(R.id.caption_text))
              .setText(&quot;Preparing the recognizer&quot;);
  
      lv = (ListView) findViewById(R.id.lvVoiceReturn);
      tv = (TextView) findViewById(R.id.result_text);
      a = (EditText) findViewById(R.id.TFusername);
      b = (Button) findViewById(R.id.bVoice);
      c = (Button)findViewById(R.id.Blogin);
  
      // Recognizer initialization is a time-consuming and it involves IO,
      // so we execute it in async task
  
      new AsyncTask&lt;Void, Void, Exception&gt;() {
          @Override
          protected Exception doInBackground(Void... params) {
              try {
                  Assets assets = new Assets(PocketSphinxActivity.this);
                  File assetDir = assets.syncAssets();
                  setupRecognizer(assetDir);
              } catch (IOException e) {
                  return e;
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(Exception result) {
              if (result != null) {
                  ((TextView) findViewById(R.id.caption_text))
                          .setText(&quot;Failed to init recognizer &quot; + result);
              } else {
                  switchSearch(KWS_SEARCH);
              }
          }
      }.execute();
  //line added.../////////////////////////
      a.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              if (s.toString().trim().equalsIgnoreCase(&quot;open voice command&quot;)) {
                  //
                  //Do your stuff here OR button.performClick()
                  //
  
                  //DELAY
                  Handler handler = new Handler();
                  handler.postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          if (!isDone) {
                              b.performClick();
                              isDone = true;
                          }
                      }
                  }, 500);
              }
          }
  
          @Override
          public void afterTextChanged(Editable s) {
  
          }
      });
  ////////////////////////////////////////
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      recognizer.cancel();
      recognizer.shutdown();
  }
  
  /**
   * In partial result we get quick updates about current hypothesis. In
   * keyword spotting mode we can react here, in other modes we need to wait
   * for final result in onResult.
   */
  @Override
  public void onPartialResult(Hypothesis hypothesis) {
      if (hypothesis == null)
          return;
  
      String text = hypothesis.getHypstr();
      //((TextView) findViewById(R.id.result_text)).setText(text);
      ((EditText) findViewById(R.id.TFusername)).setText(text);
  }
  
  /**
   * This callback is called when we stop the recognizer.
   */
  @Override
  public void onResult(Hypothesis hypothesis) {
      //((TextView) findViewById(R.id.result_text)).setText(&quot;&quot;);
      ((EditText) findViewById(R.id.TFusername)).setText(&quot;&quot;);
      if (hypothesis != null) {
          String text = hypothesis.getHypstr();
          makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
  
  
      //a.setText((String) tv.getText());
          //tv = TextView.getText().toString();
      }
  }
  
  @Override
  public void onBeginningOfSpeech() {
  }
  
  /**
   * We stop recognizer here to get a final result
   */
  @Override
  public void onEndOfSpeech() {
      if (!recognizer.getSearchName().equals(KWS_SEARCH))
          switchSearch(KWS_SEARCH);
  }
  
  private void switchSearch(String searchName) {
      recognizer.stop();
  
      // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
      if (searchName.equals(KWS_SEARCH))
          recognizer.startListening(searchName);
      else
          recognizer.startListening(searchName, 10000);
  
      String caption = getResources().getString(captions.get(searchName));
      ((TextView) findViewById(R.id.caption_text)).setText(caption);
  }
  
  private void setupRecognizer(File assetsDir) throws IOException {
      // The recognizer can be configured to perform multiple searches
      // of different kind and switch between them
  
      recognizer = defaultSetup()
              .setAcousticModel(new File(assetsDir, &quot;en-us-ptm&quot;))
              .setDictionary(new File(assetsDir, &quot;cmudict-en-us.dict&quot;))
  
                      // To disable logging of raw audio comment out this call (takes a lot of space on the device)
              .setRawLogDir(assetsDir)
  
                      // Threshold to tune for keyphrase to balance between false alarms and misses
              .setKeywordThreshold(1e-45f)
  
                      // Use context-independent phonetic search, context-dependent is too slow for mobile
              .setBoolean(&quot;-allphone_ci&quot;, true)
  
              .getRecognizer();
      recognizer.addListener(this);
  
      /** In your application you might not need to add all those searches.
       * They are added here for demonstration. You can leave just one.
       */
  
      // Create keyword-activation search.
      recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
  
  }
  
  @Override
  public void onError(Exception error) {
      ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
  }
  
  @Override
  public void onTimeout() {
      switchSearch(KWS_SEARCH);
  }
  
  //Assign button clicks to go to a new activity:
  public void onButtonClick_1(View v){
      if (v.getId() == R.id.bVoice){
          String str_1 = a.getText().toString();
  
          //Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'command' which is not case sensitive
          if (str_1.toLowerCase().contains(&quot;command&quot;)) {
              Intent userintent = new Intent(PocketSphinxActivity.this, Gvoice.class);
  
              startActivity(userintent);
          } else {
              Toast.makeText(getApplicationContext(), &quot;Incorrect Information&quot;, Toast.LENGTH_SHORT).show();
          }
      }
  }
  
  //Added this to clear the 'open voice command' text when pressing back from a activity window
  //This at the moment seems to only work when typing the command, If I use speech to text to say'open voice command
  @Override
  public void onResume(){
      super.onResume();
      isDone = false;
      a.setText(&quot;&quot;);
  }
  
  }
  </code></pre>
  <p>Additionally, due to using a boolean method, if I go back to this activity using the back button etc in my app, it does not seem to trigger the option again.  Is there a way to cause it to trigger <code>b</code> again if I return back to the same window/activity?</p>
  <p>I'll really appreciate some useful help in modifying part of my code shown above.</p>