Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

Countdown timer that that increases speed after button press

I am trying to create a countdown timer that will increases its speed after a button press I uses the counter also to adjust a progress bar.

Right now I am adjusting the speed (increasing) after the button is pressed but it does not start from the beginning. For example, when I start my program the timer start from the beginning and decreases progressively, which is fine. However, when I press the button the counter does not start from the beginning like this: enter image description here enter image description here enter image description here

I want just to make it run faster after each button press, not to decrease the length.

this is my code:

 mTrueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //is the user right? if pressing True button
            if(isMathProblemTrue == 1){
                //user is correct
                Toast.makeText(MainActivity.this,"Correct!",Toast.LENGTH_SHORT).show();
                generateMathProblem();
                timer_length *= 0.8;
                timer_interval *= 0.8;
                Log.d(TAG,"time length:"+timer_length);
                Log.d(TAG,"time interval:"+timer_interval);
                mCountDownTimer.cancel();
                createNStartTimer();
                //restartTimer();
            }else{
                //user is incorrect
                transferUserToStartScreen();
                //reset the timer
                mCountDownTimer.cancel(); // cancel
            }
        }
    });
private void createNStartTimer() {
    mCountDownTimer = new CountDownTimer(timer_length,timer_interval) {
        @Override
        public void onTick(long millisUntilFinished) {
            Log.d(TAG,"Mil until finish:"+millisUntilFinished);
            int progress = (int) (millisUntilFinished/100);
            mProgressBar.setProgress(progress);
        }
        @Override
        public void onFinish() {
            mProgressBar.setProgress(0);
            transferUserToStartScreen();
        }
    }.start();
}

Upvotes: 0

Views: 794

Answers (1)

Emad Y
Emad Y

Reputation: 425

create 2 global constants outside the functions

int totalMillisUntilFinished = 0;
bool firstTime = true;

we initialize the totalMillisUntilFinished when onTick is called for the time, so update your your onTick function:

private void createNStartTimer() {
    firstTime = true;
    mCountDownTimer = new CountDownTimer(timer_length,timer_interval) {
        @Override
        public void onTick(long millisUntilFinished) {
            if(firstTime){totalMillisUntilFinished = millisUntilFinished; firstTime = false;}
            Log.d(TAG,"Mil until finish:"+millisUntilFinished);
            int progress = (int) (millisUntilFinished*100/totalMillisUntilFinished);
            mProgressBar.setProgress(progress);
        }

Personally, I use Handlers and Runnables, which I would definitely suggest looking into instead.

Upvotes: 1

Related Questions