title: How I can stop thread in Android
body: <p>I tried to make a countdown counter where the user clicking a button stopped the countdown.</p>
  
  <p>I searched the internet for a method to stop the thread controlling the countdown.</p>
  
  <p>I attempted to use <code>interrupt</code>, <code>Interrupted</code> and <code>stop</code> but was not successful in  stopping the thread using any of these methods.</p>
  
  <p>Question:
  How can I stop the thread controlling my countdown counter?</p>
  
  <pre><code>public class Main2Activity extends AppCompatActivity {
  
      TextView mTextField;
      Button stop;
      Activity myActivity;
      PrimeThread T1;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main2);
          myActivity=this;
          mTextField = (TextView) findViewById(R.id.counter);
          T1=new PrimeThread();
          T1.start(); }
  
      public void stop(View view){   T1.interrupt();    }
  
      class PrimeThread extends Thread {
          int i=10;
          public void run() {
              while(i&gt;0){
                  myActivity.runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          mTextField.setText("00:0"+i);
                      }
                  });
                  i--;
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }}}
  }
  </code></pre>