Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting countdown timer with 00:00:00 not working. Displays 00:00:00 until paused then displays the time remaining
    primarykey
    data
    text
    <p>I was trying to create a countdown timer from <a href="http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/" rel="noreferrer">http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/</a> 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.</p> <pre><code> 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 &lt; 10) secondsD = "0" + seconds; if (minutes &lt; 10) minutesD = "0" + minutes; if (hours &lt; 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)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload