Count down timer in android with minute and second

Here is the easiest way to implement Count down timer in android.
Now let's get the timer in Minute and seconds

1-

CountDownTimer countDownTimer;
long duration = 300000;

countDownTimer = new CountDownTimer(duration,
 1000)
 {
 public void onTick(long millisUntilFinished)
 {
txtTimer.setText(""+
String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes
( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds
(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.
toMinutes(millisUntilFinished))));
    }

public void onFinish() {
txtTimer.setText("done!");
}
}.start();



To get the timer in seconds only click here


if want to stop the timer before the finish time;


countDownTimer.cancel();

No comments:

Post a Comment