Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer Android counter

Tags:

android

timer

I'm trying to implement a Timer counter, but all that I see is CountDown and I don't want to count down time, I just want start from 00:00 and every second sum one second until 60:00, I've been trying to implement this :

1.-Android timer? How? - Answer

2.- Chronometer

And they didn't help me at all.. can you guide me how to start time from 00:00 and end in 60:00?

Answer here :

As Usama Zafar put his answer, I updated the code to control this CountDownTimer in case you'll need it...

I created

CountDownTimer cdtTimer;

Then the method I have to startTimer() that starts to count the time it's like this :

private void StartTimer(){
    final long EndTime   = 3600;
    cdtTimer = new CountDownTimer(EndTime*1000, 1000) {
        public void onTick(long millisUntilFinished) {
            long secondUntilFinished = (long) (millisUntilFinished/1000);
            long secondsPassed = (EndTime - secondUntilFinished);
            long minutesPassed = (long) (secondsPassed/60);
            secondsPassed = secondsPassed%60;
            tvCounterTimer.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
        }

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

Then wherever I want to cancel this CountDownTimer I simply do

cdtTimer.cancel();
like image 223
StuartDTO Avatar asked Nov 25 '25 04:11

StuartDTO


1 Answers

The simplest way to implement Timer is to obviously use CountDown as stated on Android's official guide:

 new CountDownTimer(30000, 1000) {

      public void onTick(long millisUntilFinished) {
          mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
      }

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

But as per your requirement you don't want to count down rather you wish to count up until you reach 60min mark. How can we do it is indeed an interesting question. You can try implementing a Runnable and get system time in milliseconds on each tick using: System.currentTimeMillis().

It tends to get complex right? So why not play with the CountDown Timer and make it work to our needs? How can we do it? Observe the below code:

 long StartTime = 0;    //Starting from 00:00
 long EndTime   = 3600;          //End at min:sec converted to seconds

 new CountDownTimer(EndTime*1000, 1000) {

      public void onTick(long millisUntilFinished) {
          long secondUntilFinished = (long) (millisUntilFinished/1000);
          long secondsPassed = (EndTime - secondUntilFinished);
          long minutesPassed = (long) (secondsPassed/60);
          secondsPassed = secondsPassed%60;
          // So now at this point your time will be: minutesPassed:secondsPassed
          mytextView.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
      }

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

The code is very easy to understand. If you still have any queries comment them and I will address them. Hope this is helpful.

like image 140
usamazf Avatar answered Nov 27 '25 20:11

usamazf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!