Arun Babu
Arun Babu

Reputation: 259

How to display a progress bar for 1 minute?

I have a horizontal progress bar in android. I need to make it complete in 60 seconds.

Why doesn't the below code work?

int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
     progressHandler.postDelayed((new Runnable() {
        public void run() {                     
           progressBarHorizontal.setProgress(progress);                    
        }                   
     }),progress * 1000);
     progress++;
}

Please suggest some other methods. I tried this:

new Thread(new Runnable() {
                int progress = 0;       
                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis() ) {

                        progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
                        // Update the progress bar
                        progressHandler.post(new Runnable() {
                            public void run() {
                                progressBarHorizontal.setProgress(progress);
                            }
                        });                         
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("tag","Progress thread cannot sleep");
                        }
                    }
                }
            }).start(); 

But it is not working either.

The second piece of code actually works, the problem was with my logic.

Upvotes: 5

Views: 11828

Answers (4)

vilpe89
vilpe89

Reputation: 4702

This one will update the statusbar until it reaches its maximum value:

private int progress = 0;
private final int pBarMax = 60;

...

final ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);     
pBar.setMax(pBarMax);
final Thread pBarThread = new Thread() {
    @Override
    public void run() {
        try {
            while(progress<=pBarMax) {
                pBar.setProgress(progress);
                sleep(1000);
                ++progress; 
            }
        }
        catch(InterruptedException e) {
        }
    }
};

pBarThread.start();

Upvotes: 3

Arun Babu
Arun Babu

Reputation: 259

This code works for me

new Thread(new Runnable() {

                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis()) {

                        progress = 60 - (int) (timerEnd - System.currentTimeMillis()) / 1000;
                        // Update the progress bar

                        progressHandler.post(new Runnable() {
                            public void run() {                     
                                    progressBarHorizontal.setProgress(progress);                    
                            }                   
                        });

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("App","Progress thread cannot sleep");
                        }
                    }
                    progressHandler.post(new Runnable() {
                        public void run() {                     
                                okButton.performClick();                    
                        }                   
                    });
                }
            }).start(); 

Upvotes: 2

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

You can try this code for that:

 Thread timer = new Thread(){
public void run(){
    try{
        sleep(10000);
        while(progressBarStatus < 10000){
            StartPoint.this.runOnUIThread(new Runnable(){
                public void run()
                {
                    progressBar.setProgress(progressBarStatus);
                    progressBarStatus += 1000;
                }
            });

        }
    }catch(InterruptedException e){
        e.printStackTrace();
    }finally{

    }
}
};
timer.start();

Upvotes: 2

Houcine
Houcine

Reputation: 24181

you can try the CountDownTimer :

pd = ProgressDialog.show(MovementEntry.this, "", "Please Wait...",
                true, false);
pd.show();
new CountDownTimer(60000, 1000) {
  @Override
  public void onTick(long millisUntilFinished) {
      //this will be done every 1000 milliseconds ( 1 seconds )
      int progress = (60000 - millisUntilFinished) / 1000;
      pd.setProgress(progress);
  }

   @Override
   public void onFinish() {
      //the progressBar will be invisible after 60 000 miliseconds ( 1 minute)
      pd.dismiss();
   }

}.start();

Upvotes: 7

Related Questions