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.
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();
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With