Search code examples
androidcountdowntimer

Pausing a Android countdowntimer with same button


I have found numerous examples of how to pause a countdowntimer in Android, but each of these examples utilises more than 1 button (pause, resume and cancel).

I want to have one button that when I press it the timer starts, then when I press it again it pauses (cancelling the original timer, capturing the timer value) and resumes when clicked again (taking the captured time from the pause to start off a new counterdowntimer).

Does anyone have an example of how to achieve this? I have tried if else loops in the onClick listener of the button. I have a very crude semi-working example;

if (gameOn == 1) {
 if((clkOnTimerBtn % 2)==0) {
    isPaused = true; // PAUSE COUNTDOWN TIMER
    resumeCountDownTimer(view, "pause");
 } else { // RESUME COUNTDOWN TIMER
   resumeCountDownTimer(view, "resume");
}

The problem with the above is that this is carried out in the button onclick listener, so if a new CountDownTimer is created inside my resumeCountDownTimer its not possible to access the timer later on to cancel it (pause). I have also tried looking for a way to cancel all countdowntimers, messy if I could, but I couldn't find any examples or references to doing so as this would at least get the desired behaviour even if it isn't the most elegant way.


Solution

  • If I understood you correctly, something like this should work.

    public class MainActivity extends AppCompatActivity {
    
    CountDownTimer countDownTimer;
    long duration = 100000; //This is the initial time,
    long millisecondsLeft = 100000; // This is the time left. At the start it equales the duration.
    boolean isCountDownTimerActive = false;
    Button startButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        start = (Button) findViewById(R.id.startStop);
        final TextView timeLeft = (TextView) findViewById(R.id.timeLeft);
    
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCountDownTimerActive) {
                    if (countDownTimer != null)
                        countDownTimer.cancel();
                    isCountDownTimerActive = false;
                } else {
                    countDownTimer = new CountDownTimer(millisecondsLeft, 1000) {
                        @Override
                        public void onTick(long l) {
                            millisecondsLeft = l;
                            timeLeft.setText(" " + l);
                        }
    
                        @Override
                        public void onFinish() {
    
                        }
                    };
                    isCountDownTimerActive = true;
                    countDownTimer.start();
                }
            }
        });
    }
    

    }