Android Chronometer format hh:mm without seconds

2.7k Views Asked by At

I'm trying to create a chronometer in android, using android.widget.Chronometer, that displays only hours and minutes (hh:mm), but untill now I didn't find a way to make it. I don't even know if this is possible with this widget. Is this possible? If this is possible can you please show me how to do it?

2

There are 2 best solutions below

0
On

Try this

Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        public void onChronometerTick(Chronometer cArg) {
            long time = SystemClock.elapsedRealtime() - cArg.getBase();
            int h = (int) (time / 3600000);
            int m = (int) (time - h * 3600000) / 60000;
            cArg.setText(String.format("%02d:%02d", h, m));
        }
});
chronometer.start();
0
On

I was too having this problem and i found that if you want chronometer to show 00:00:52(for 52 seconds of timecount),it will just show 52 only not the hh and mm field until they have some value to be displayed i.e. chronometer will show the mm field if the mm field have some value, it the the behavior of chronometer as far i found, so if you want all fields to be shown in every case then you can use timer like this, hope it will help you.