20

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?

Nikki
  • 3,314
  • 13
  • 36
  • 59

7 Answers7

45

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();
kinske
  • 597
  • 8
  • 24
Md. Naushad Alam
  • 8,011
  • 6
  • 25
  • 23
18

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
7

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

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
4

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.

MatejC
  • 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
1
// 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();
Community
  • 1
  • 1
Arun PS
  • 4,610
  • 6
  • 41
  • 56
0

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

Marurban
  • 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
  • 2
    This 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
0

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

Community
  • 1
  • 1
Daniel
  • 502
  • 2
  • 9
  • 18
  • 1
    i 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