title: Android progressbar with runnable interface
body: <p>I have a code like the below:</p>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout   
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent"     
       android:layout_height="fill_parent"     
       android:orientation="vertical" &gt;
      &lt;ProgressBar 
          android:id="@+id/progressbar"     
          android:layout_width="wrap_content"        
          android:layout_height="wrap_content" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>BasicViews2Activity.java</strong></p>
  
  <pre><code>public class BasicViews2Activity extends Activity 
  {     
    static int progress;     
    ProgressBar progressBar;     
    int progressStatus = 0;     
    Handler handler = new Handler();
  
    /** Called when the activity is first created. */     
    @Override     
    public void onCreate(Bundle savedInstanceState) 
    {         
      super.onCreate(savedInstanceState);          
      setContentView(R.layout.main);                 
      progress = 0; 
      progressBar = (ProgressBar) findViewById(R.id.progressbar);
      //---do some work in background thread--        
      new Thread(new Runnable()        
      {             
        public void run()            
        {                 
          //---do some work here--  
          while (progressStatus &lt; 10)              
          {  
            progressStatus = doSomeWork();   
          }
          //---hides the progress bar---
          handler.post(new Runnable()         
          { 
            public void run()                      
            {  
              progressBar.setVisibility(View.GONE);                      
            }                   
          });             
        }
  
        //---do some long running work here--            
        private int doSomeWork()             
        {                 
          try {    
            Thread.sleep(500);   
          } catch (InterruptedException e)                 
          {                     
            e.printStackTrace();   
          }                 
          return ++progress;             
        }         
      }).start();     
    } 
  }
  </code></pre>
  
  <p>Why did we use two runnable objects inside the thread class to cancel the progressbar? Couldn't we do it inside the first runnable obj – just after while loop, without another object?</p>