How to make an horizontal progress bar depend on seconds in Android?
Asked Answered
N

3

7

I want to know how to make an horizontal progress bar depend on seconds. For example, what code I have to write to the progress bar start in 0% at 0 seconds and reach 100% after 60 seconds?

Resuming I want to make an horizontal progress bar that only depends on seconds and nothing else.

Neologism answered 16/6, 2011 at 19:42 Comment(0)
I
11
bar = (ProgressBar) findViewById(R.id.progress);
    bar.setProgress(total);
    int oneMin= 1 * 60 * 1000; // 1 minute in milli seconds

    /** CountDownTimer starts with 1 minutes and every onTick is 1 second */
    cdt = new CountDownTimer(oneMin, 1000) { 

        public void onTick(long millisUntilFinished) {

            total = (int) ((timePassed/ 60) * 100);
            bar.setProgress(total);
        }

        public void onFinish() {
             // DO something when 1 minute is up
        }
    }.start();

i have edited the code. see it now. so how this works. first you set a total on the progress bar which in your case will be 60. then you need to calculate the percentage of how much time has passed since the start and that you get with timePassed/60*100 and casting it to int. so on every tick you increase the progress by 1/100 of the total size. Hope this makes it more clear.

Iinden answered 16/6, 2011 at 19:45 Comment(1)
what is the variable timePassed?Symer
B
6

This answer is modified from above answer.

    progressBar = (ProgressBar) findViewById(R.id.progressbar);

    // timer for seekbar
        final int oneMin = 1 * 60 * 1000; // 1 minute in milli seconds

        /** CountDownTimer starts with 1 minutes and every onTick is 1 second */
        new CountDownTimer(oneMin, 1000) {
            public void onTick(long millisUntilFinished) {

                //forward progress
                long finishedSeconds = oneMin - millisUntilFinished;
                int total = (int) (((float)finishedSeconds / (float)oneMin) * 100.0);
                progressBar.setProgress(total);

//                //backward progress
//                int total = (int) (((float) millisUntilFinished / (float) oneMin) * 100.0);
//                progressBar.setProgress(total);

            }

            public void onFinish() {
                // DO something when 1 minute is up
            }
        }.start();
Banish answered 8/5, 2018 at 13:39 Comment(0)
R
1

I'm assuming you have some code that is counting to 60 already. Based on that:

int time; // 0-60 seconds
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setProgress((time / 60.0) * 100);
Roca answered 16/6, 2011 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.