5

I was trying to create a countdown timer from http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/ but I cannot get it to work with the format I want 00:00:00 as described in the article. The timer will start and allow me to pause it and it will run for two minutes but you cannot see it running. If you pause it you can see how much time is remaining but you I cannot get it to work. Without the formatting it works fine and counts down. Does anyone have this working or know of how to fix it? I have been searching around and cannot find anything covering a countdown timer like this with formatting. Any help would be appreciated.

  TextView timeDisplay;
  MyCount counter;
  int state = 0;
  int length = 120000;
  long startTime = 0;
  long currentTime = 0;
  long timeElapsed = 0;
  long timeRemaining = 0;
  long prevTimeRemaining = 0;
  Button control;

    public String formatTime(long millis) {
          String output = "00:00:00";
          long seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;

          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 60;

          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD = String.valueOf(hours); 

          if (seconds < 10)
            secondsD = "0" + seconds;
          if (minutes < 10)
            minutesD = "0" + minutes;
          if (hours < 10)
            hoursD = "0" + hours;

          output = hoursD + " : " + minutesD + " : " + secondsD;
          return output;
        }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countdown);

    timeDisplay = (TextView) findViewById(R.id.timer);
    control = (Button) findViewById(R.id.control);
    counter = new MyCount (length, 1000);
  }

  public void control(View view) {
    switch (state) {
    case 0:
      startTime = System.currentTimeMillis();
      counter.start();
      control.setText(R.string.pause);
      state = 1;
      break;
    case 1:
      // pause
      currentTime = System.currentTimeMillis();
      timeElapsed = currentTime - startTime;
      if (prevTimeRemaining == 0)
        timeRemaining = length - timeElapsed;
      else
        timeRemaining = prevTimeRemaining - timeElapsed;
      counter.cancel();
      timeDisplay.setText("" + formatTime(timeRemaining));
      control.setText(R.string.resume);
      prevTimeRemaining = timeRemaining;

      // resume
      counter = new MyCount(timeRemaining, 1000);
      state = 0;
      break;
    case 2:
      prevTimeRemaining = 0;
      counter = new MyCount(length, 1000);
      control.setText(R.string.start);
      timeDisplay.setText(R.string.timer);
      state = 0;
    }
  }

  public class MyCount extends CountDownTimer {

    public MyCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }

    public void onFinish() {
      timeDisplay.setText("done!");
      state = 2;
     control.setText(R.string.restart);
    }

    public void onTick (long millisUntilFinished) {
      timeDisplay.setText ("Left: " + formatTime(timeRemaining));

    }

  }

}
Daniel
  • 502
  • 2
  • 9
  • 18
  • 2
    I got it working. Originally timeDisplay.setText("Left: " + formatTime(timeRemaining)); was timeDisplay.setText("Left: " + millisUntilFinished / 100); and I had tried putting formatTime in front of millisUntilFinished with no success but I tried without the / 100 and it works – Daniel Oct 17 '10 at 01:29
  • 7
    Then please please answer and check your own answer, so that this question is removed from the unanswered list. – rds Nov 28 '11 at 20:00

1 Answers1

0

The '+' operator has higher precedence than the division. Thus you try to divide a string by 100. You can use parenthesis to imply order.

http://bmanolov.free.fr/javaoperators.php

mach
  • 8,315
  • 3
  • 33
  • 51