Matteo Santini
Matteo Santini

Reputation: 23

Android Studio: Need help animating a progressbar

I have a round progress bar, made with a drawable, with values from 0-100 (included). When I load my activity, I want to animate the progressbar being "filled" with values from 0 to my desidered value. I'm using a CountdownTimer for that. The issue is, even though I set the value to 100, the max (or any other value), the progressbar doesn't get filled all the way. Here's my code:

  seekbar = (ProgressBar) findViewById(R.id.progressBar);
    seekbar.setMax(100);
    final int desiredvalue=100;


     CountDownTimer countDownTimer =
            new CountDownTimer(1500, 20) {
             int startingvalue=0;
                public void onTick(long millisUntilFinished) {
                    float progressPercentage = (desiredvalue*20)/1500;
                    seekbar.setProgress(startingvalue+=progressPercentage);

                }

                @Override
                public void onFinish() {

                }
            };
     countDownTimer.start();
}

This is what's happening: Open me!

Upvotes: 2

Views: 409

Answers (2)

Ananta Raha
Ananta Raha

Reputation: 1071

I think just a small modification to your code will fix this. Just change your onTick code like the following.

seekbar = (ProgressBar) findViewById(R.id.progressBar);
seekbar.setMax(100);
final int desiredvalue=100;
CountDownTimer countDownTimer = new CountDownTimer(1500, 20) {
    public void onTick(long millisUntilFinished) {
        float progressPercentage = (desiredvalue*(1500L-millisUntilFinished))/1500;
        seekbar.setProgress(progressPercentage);
    }

    @Override
    public void onFinish() {
        
    }
};
countDownTimer.start();

Upvotes: 0

CodeWithVikas
CodeWithVikas

Reputation: 1443

As i understood you want set progress value based on your requirement.

You can use ValueAnimator something like this :

progressBar = findViewById(R.id.my_pb);

        ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
        animation.setDuration(1000);
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator updatedAnimation) {
                
                Integer animatedValue = Math.round( (float)updatedAnimation.getAnimatedValue());

                progressBar.setProgress(animatedValue);
            }
        });
        animation.start();

For more Detail About ValueAnimator

Upvotes: 1

Related Questions