How to display a progress bar for 1 minute?

Android has a horizontal progress bar. I need to do this in 60 seconds.

Why is this code not working?

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

Please suggest 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 doesn’t work either.

The second part of the code really works, the problem was in my logic.

+4
source share
4 answers

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(); 
+1
source

you can try 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(); 
+5
source

This file will update the status bar to the 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(); 
+3
source

You can try this code for this:

  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(); 
+2
source

Source: https://habr.com/ru/post/1484365/


All Articles