Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Chronometer format hh:mm without seconds

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?

like image 576
Tiz Avatar asked Jul 18 '26 07:07

Tiz


1 Answers

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();
like image 102
GerryP Avatar answered Jul 20 '26 20:07

GerryP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!