title: How do we use runOnUiThread in Android?
body: <p>I'm new to Android and I'm trying to use the UI-Thread, so I've written a simple test activity. But I think I've misunderstood something, because on clicking the button - the app does not respond anymore</p>
  
  <pre class="lang-java prettyprint-override"><code>public class TestActivity extends Activity {
  
      Button btn;
      int i = 0;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          btn = (Button)findViewById(R.id.btn);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  runThread();
              }
          });
      }
  
      private void runThread(){
          runOnUiThread (new Thread(new Runnable() {  
              public void run() {
                  while(i++ &lt; 1000){
                      btn.setText("#"+i);
                      try {
                          Thread.sleep(300);
                      } 
                      catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
               }
          }));
      }
  }
  </code></pre>