Search code examples
javaandroidchronometer

How to use Chronometer on Android?


I want to create a countdown of 10 seconds. When countdown finishes calling a function.

enter code here

Thanks!


Solution

  • It looks like there is already an api for just that:

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

    Reference: http://developer.android.com/reference/android/os/CountDownTimer.html