null
null

Reputation: 21

The logic to make a progress bar countdown timer count "up" in Android

I can't seem to wrap my head around how I should make a progress bar fill up. I am using a countdown timer to update the progress on a progress bar making it count down until it reaches zero. Now I want to reverse the progress and have it start at zero and fill up, but how would I go about achieving that?

public class theCountDownTimer extends CountDownTimer
{
    public theCountDownTimer(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        int progress = (int) (millisUntilFinished/100);
        progressBarCounter.setProgress(progress);
    }

    @Override
    public void onFinish()
    {
        Button nextInstance = (Button) findViewById(R.id.run);
        nextInstance.performClick();
    }
}

Upvotes: 0

Views: 1390

Answers (1)

TDG
TDG

Reputation: 6161

The progressBar default maximum value is 100. I guess that you want it to progress every tick, so inside theCountDownTimer you should define:

progressBarCounter.setMax((int) millisInFuture/1000);

That way, if you're counting 80 seconds ( = 80,000mS), then the bar will range between 0 and 80.
Next, you can use incrementProgressBy (int diff) method:

public class theCountDownTimer extends CountDownTimer
{

  public theCountDownTimer(long millisInFuture, long countDownInterval)
  {
    super(millisInFuture, countDownInterval);
    progressBarCounter.setMax((int) millisInFuture/1000);
    progressBarCounter.setProgress(0);  //Reset the progress
  }

  @Override
  public void onTick(long millisUntilFinished)
  {
    progressBarCounter.incrementProgressBy(1);
  }

  @Override
  public void onFinish()
  {
    Button nextInstance = (Button) findViewById(R.id.run);
    nextInstance.performClick();
  }
}

I guess that you also have an error here -

int progress = (int) (millisUntilFinished/100);

Didn't you mean to divide by 1000 instead 100?

Upvotes: 1

Related Questions