Skip to content Skip to sidebar Skip to footer

How To Get Seconds From Progressbar

I have a circular progress bar that at the moment is working fine, but now I need to add like a TextView or something to know how many seconds left. This is my ProgressBar

Solution 1:

I would suggest using a CountDownTimer instead. Create a CountDownTimer with your desired duration, say 5 seconds, and then on each tick, update the ProgressBar and the TextView. It will give you the duration remaining.

    // Creating a CountDownTimer for 5 seconds, that will call onTick every second.

final int totalTime = 5000;
new CountDownTimer(totalTime, 100) {

    public void onTick(long millisUntilFinished) {
        long secondsRemaining = ( millisUntilFinished / 1000);
        // Updating the TextView with seconds remaining
        textView.setText(String.valueOf(secondsRemaining));

        // Calculating the progress out of 100.
        int progress = (int) millisUntilFinished / totalTime;

        // Creating an animation to smoothly update the progress bar between each tick.
        ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), progress);
        animation.setDuration(100);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.start(); 
    }

    public void onFinish() {
        // done
    }

}.start();

Post a Comment for "How To Get Seconds From Progressbar"