Android ProgressBar: Starting, and Stopping Them


Ah, just spent a while figuring this out. You have a progress bar, you’d like it to start, and, you’d like it to stop. There are lots of examples of start a progress bar, but very little on how to stop, or interrupt it. That’s the key- you are interrupting a running while loop.

The idea is that in one method (which the Start button launches) you begin the thread that starts the progressbar animation. This isn’t a simple background, or asynchronous task, as you need to constantly update the user interface.

So make sure to set the thread as a property: in mine, it’s “mThreadProgress.” In the other button, you send an interrupt to that thread, “mThreadProgress.interrupt()”. The thread’s run() method has an exception catch for interrupt, so when that happens, I ‘break’ out of the while. In your “stop” method, adjust the interface to reflect a zero’ed out, or stopped progressbar by resetting the value to zero. While the thread is active (and it’s always active) the while() loop will keep checking, so make sure to add that added conditional, “!mThreadProgress.isInterrupted()”.

package com.banane.unicorn;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class UnicornActivity extends Activity {

   ProgressBar progressBar;
    ThreadProgress mThreadProgress;
    int progressValue = 0;
   
       
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    }
   
    public void startMe(View v){
        // start progress bar
        progressValue = 0;
        progressBar.setProgress(progressValue);
        mThreadProgress = new ThreadProgress();
        mThreadProgress.start();
       
    }
   
   
    Handler handler = new Handler(new Callback() {

        @Override
        public boolean handleMessage(final Message msg) {
            runOnUiThread(new Runnable() {
                 public void run() {
                     progressBar.setProgress(msg.arg1);
                 }
             });
            return false;
        }
    });

    public class ThreadProgress extends Thread implements Runnable {
       
        @Override
        public void run() {
               
             while( progressValue <= 15 && !mThreadProgress.isInterrupted()) {
                      try{
                          Log.d("unicorn","in while loop");
                                  progressValue++;
                                  Message message = new Message();
                                  message.arg1 = progressValue;
                                  handler.sendMessage(message);
                                  Thread.sleep(1000);
                                } catch (InterruptedException e){
                                        e.printStackTrace();
                                        break;
                                }
                }
        }

    }    
   
   
    public void stopMe(View v){
       
        mThreadProgress.interrupt();
        progressValue=0;
        progressBar.setProgress(progressValue);
        Log.d("unicorn","in stop me");

    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

    <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
               android:layout_margin="20dip"
       
       android:text="@string/hello" />

    <Button
       android:id="@+id/button1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Start"
       android:onClick="startMe"
       android:layout_margin="20dip"
       />

    <Button
       android:id="@+id/button2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="20dip"
       android:onClick="stopMe"
       android:text="Stop" />

    <ProgressBar
       android:id="@+id/progressBar1"
       style="?android:attr/progressBarStyleHorizontal"
       android:layout_width="match_parent"
       android:layout_margin="20dip"
       android:layout_height="wrap_content"
       android:max="15" />

</LinearLayout>