One of my problem is of changing the format of chronometer in android. i have the problem that chronometer shows its time in 00:00 format and i want it to come in 00:00:00 format.does anyone knows the answer?
7 Answers
Here is an easy and smart solution for time format 00:00:00 in chronometer in android
Chronometer timeElapsed = (Chronometer) findViewById(R.id.chronomete);
timeElapsed.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer cArg) {
long time = SystemClock.elapsedRealtime() - cArg.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String hh = h < 10 ? "0"+h: h+"";
String mm = m < 10 ? "0"+m: m+"";
String ss = s < 10 ? "0"+s: s+"";
cArg.setText(hh+":"+mm+":"+ss);
}
});
timeElapsed.setBase(SystemClock.elapsedRealtime());
timeElapsed.start();

- 597
- 8
- 24

- 8,011
- 6
- 25
- 23
-
3Dude, really really nice solution. Easiest way I've ever seen ;) – silvia_aut Oct 02 '13 at 08:46
-
thanks for your idea, I edited your code a little to improve performance here cArg.setText(String.format("%02d:%02d:%02d", h, m, s )); – Sruit A.Suk May 03 '15 at 12:21
-
1in there any way to do this if the chronometer is in a widget (homescreen widget)? – ArJ Jun 04 '16 at 13:33
I was dealing with the same issue. The easiest way to achieve this goal would be overriding updateText method of the Chronometer, but unfortunately it is a private method so I have done this in this way :
mChronometer.setText("00:00:00");
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
CharSequence text = chronometer.getText();
if (text.length() == 5) {
chronometer.setText("00:"+text);
} else if (text.length() == 7) {
chronometer.setText("0"+text);
}
}
});
I know that it would be better to write custom widget but for small project it can be suitable.
Setting format on chronometer allows formatting text surrounding actual time and time is formatted using DateUtils.formatElapsedTime .
Hope that helps.

- 53,910
- 52
- 193
- 240

- 189
- 1
- 3
In Kotlin, found a better solution with no memory allocation for the String every second
cm_timer.format = "00:%s"
cm_timer.setOnChronometerTickListener({ cArg ->
val elapsedMillis = SystemClock.elapsedRealtime() - cArg.base
if (elapsedMillis > 3600000L) {
cArg.format = "0%s"
}
else {
cArg.format = "00:%s"
}
})
where cm_timer
is Chronometer

- 13,761
- 4
- 85
- 82
Chronometers setFormat method is used to add formated text like:
"Time %s from start"
resulting
"Time 00:00 from start"
In Chronometer you can not select between formats HH:MM:SS or MM:SS.

- 2,167
- 1
- 18
- 25
-
So this means there is no way you can set a format which end result is "00:00:00" without doing some manipulations in `ChronometerTickListener` as other answers suggest. isn't it? – Edijae Crusar Dec 27 '17 at 08:25
// You can use this code. Setting the format and removing it
//Removing the format (write this in oncreate)
if (mChronometerformat) {
mSessionTimeChro.setOnChronometerTickListener(new OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if (chronometer.getText().toString()
.equals("00:59:59"))
chronometer.setFormat(null);
}
});
}
// Setting the format
mSessionTimeChro.setBase(SystemClock.elapsedRealtime());
if (diffHours == 0) {
mSessionTimeChro.setFormat("00:%s");
mChronometerformat = true;
}
mSessionTimeChro.start();
Why just not to use Date class??
timeElapsed.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer cArg) {
long time = SystemClock.elapsedRealtime() - cArg.getBase();
Date date = new Date(time);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateFormatted = formatter.format(date);
cArg.setText(dateFormatted);
}
});
Simple and clean

- 1,575
- 1
- 9
- 9
-
The Chronometer class expects a specific pattern in its setText(String) method. Your code will raise an Exception. – Franziskus Karsunke Jan 29 '15 at 13:28
-
@FranziskusKarsunke Did you check this pattern? **By default it will display the current timer value in the form "MM:SS" or "H:MM:SS", or you can use setFormat(String) to format the timer value into an arbitrary string.** – Marurban Jan 29 '15 at 13:58
-
2This works, but it is more efficient to work with the time as a long rather than instantiating a new Date and DateFormat object every second. – k2col Oct 30 '15 at 18:10
I was working on a countdown timer that used this format but it was for HH:MM:SS. Is that what youre looking for or are you looking for MM:SS:milliseconds. You could work with this code and make it count up from 0 instead of down. Here is the link - Formatting countdown timer with 00:00:00 not working. Displays 00:00:00 until paused then displays the time remaining and this might help - Show milliseconds with Android Chronometer
-
1i m looking for HH:MM:SS format.........bt chronometer doesn't show it when it is in seconds or minutes as 00:00:00 format.it only shows 00:00 when countdown is in sec. or min. – Nikki Nov 15 '10 at 03:55