Android Chronometer Format

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

+6
android
source share
5 answers

first sentence - left for history only

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

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


Change - it doesn’t work at all! Sorry for the quick, unchecked answer ... Here's what 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)); } }); 
+14
source share

It seems that the people who posted the previous answers did not even try to offer what they suggested. It just doesn't work the way they are described.
Refer to how to change the chronometer format? for more appropriate answers.

+8
source share

After some testing, I came up with this code. It is not fully tested, but you can provide me more information 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, for example.

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

I will be back in a day to check if it works, or if it needs to be called also after changing the text size. If you are a cautious person, I suggest you name him in the same context with c.start() and c.stop()

 if(ticking){ c.stop(); formatChronometerText(c); } else { formatChronometerText(c); c.start() } 
+6
source share

It 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
source share

found the best solution without allocating memory for a row 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"); } } }); 
0
source share

All Articles