Reputation: 277
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();
Upvotes: 1
Views: 735
Reputation: 11
You can try this
val min: Long = millisUntilFinished / 1000 / 60
val sec: Long = millisUntilFinished / 1000 % 60
if (sec < 10) {
binding?.value?.text = "0$min:0$sec"
} else {
binding?.value.text =" 0$min:$sec"
}
Upvotes: 0
Reputation: 21087
You can use android Chronometer to display countdown.
http://developer.android.com/reference/android/widget/Chronometer.html
Upvotes: 0
Reputation: 1316
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");
Upvotes: 1
Reputation: 2707
@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);
}
Upvotes: 1