11

How can I set Android Chronometer format to HH:MM:SS??

Axarydax
  • 16,353
  • 21
  • 92
  • 151
Altaf
  • 5,150
  • 10
  • 39
  • 55
  • 1
    possible duplicate of [how to change format of chronometer?](http://stackoverflow.com/questions/4152569/how-to-change-format-of-chronometer) – Ian Ringrose Mar 13 '11 at 22:39

5 Answers5

21

first proposal - left only for history

Chronometer c;
...
c.setFormat("HH:MM:SS");

see http://developer.android.com/reference/android/widget/Chronometer.html#setFormat%28java.lang.String%29


Edit - This does not work at all! Sorry for the too fast, untested answer... Here is something that works:

Chronometer c;
...    
c.setOnChronometerTickListener(new OnChronometerTickListener() {
    public void onChronometerTick(Chronometer cArg) {
        long t = SystemClock.elapsedRealtime() - cArg.getBase();
        cArg.setText(DateFormat.format("kk:mm:ss", t));
    }
});
Stéphane
  • 6,920
  • 2
  • 43
  • 53
8

It seems people who posted the previous answers did not even try what they suggested. It simply does not work the way they described.
Refer to how to change format of chronometer? for more appropriate answers.

Community
  • 1
  • 1
  • Thanks! And you are right, the answer had not been tested. The curious thing is that it got accepted... – Stéphane Sep 08 '11 at 10:34
6

After some testing, I came up with this code. It's not fully tested but you can provide me with more info if you

private void formatChronometerText(Chronometer c) {
    int cTextSize = c.getText().length();
    if (cTextSize == 5) {
        breakingTime.setFormat("00:%s");
    } else if (cTextSize == 7) {
        breakingTime.setFormat("0%s");
    } else if (cTextSize == 8) {
        breakingTime.setFormat("%s");
    }
}

I called this method in the onCreate() method eg.

Chronometer c = ...
...
formatChronometerText(c);
c.setText("00:00:00");

I'll be back in a day to verify if it works or it needs to be called also after the size of the text changes. If you are a precautious person I suggest that you call it in the same context with c.start() and c.stop()

if(ticking){
    c.stop();
    formatChronometerText(c);
} else {
    formatChronometerText(c);
    c.start()
}
PaulG
  • 13,871
  • 9
  • 56
  • 78
a14m
  • 7,808
  • 8
  • 50
  • 67
4

This works:

Chronometer chronometer;
chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {
    public void onChronometerTick(Chronometer c) {              
        int cTextSize = c.getText().length();
        if (cTextSize == 5) {
            chronometer.setText("00:"+c.getText().toString());
        } else if (cTextSize == 7) {
            chronometer.setText("0"+c.getText().toString());
        }
    }
});
4

found a better solution with no memory allocation for the String every second:

 c.setFormat("00:%s");
 c.setOnChronometerTickListener(new OnChronometerTickListener() {
        public void onChronometerTick(Chronometer c) {
            long elapsedMillis = SystemClock.elapsedRealtime() -c.getBase();
            if(elapsedMillis > 3600000L){
                c.setFormat("0%s");
            }else{
                c.setFormat("00:%s");
            }
        }
 });
MLS
  • 237
  • 1
  • 5