Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 232
Arun Babu Avatar asked Jun 04 '13 09:06

Arun Babu


3 Answers

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();
like image 121
Houcine Avatar answered Nov 06 '22 10:11

Houcine


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();
like image 3
vilpe89 Avatar answered Nov 06 '22 10:11

vilpe89


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();
like image 2
Sagar Maiyad Avatar answered Nov 06 '22 10:11

Sagar Maiyad