1

right now I have my timer displaying seconds, what can I add to it to display the time in a 0:00 format?

new CountDownTimer((300 * 1000), 1000) {

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

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                         }
                      }.start();
Prince
  • 20,353
  • 6
  • 39
  • 59
user3224105
  • 267
  • 1
  • 2
  • 8

3 Answers3

1
@Override
public void onTick(long millisUntilFinished) {
 long temp_long = millisUntilFinished / 1000;

 second = temp_long % 60;
 hour = temp_long / 3600;
 minute = (temp_long / 60) % 60;
 String tempTimer;

 tempTimer = ((hour < 10) ? "0" + hour : "" + hour)+ ((minute < 10) ? ":0" + minute : ":"+ minute)+ ((second < 10) ? ":0" + second : ":" + second);

 mTextField.setText(tempTimer);
}
PankajAndroid
  • 2,689
  • 3
  • 27
  • 41
1

Try this:

Date date = new Date((300 * 1000)* 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);

You can also use

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
Ari
  • 1,296
  • 1
  • 18
  • 36
  • Im a bit confused, can you apply that into the countdowntimer I posted at the start? thanks a lot! – user3224105 Jan 30 '14 at 06:20
  • Nop, if you are interested in using CountdownTimer only then you have to use millisecond format only, but if you want to show a textview then inside the tick you are having the parameter millisUntilFinished, place that inside the date instead of (300*1000)*1000.....Please upvote and accept my answer if you got what you wanted – Ari Jan 30 '14 at 06:29
0

You can use android Chronometer to display countdown.

http://developer.android.com/reference/android/widget/Chronometer.html

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57