Search code examples
androidhandlercountdowncountdowntimer

Display the remaining time of the handler


I would like to know how to display the time remaining in my handler. When I click a button, I run my handler for x seconds, and I want to display a countdown on the screen before the end of the handler.


Solution

  • official documentation

    Example of showing a 30 second countdown in a text field:

    new CountDownTimer(30000, 1000) {
         public void onTick(long millisUntilFinished) {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
    }.start();