Chronometer display value greater than 60 minutes in MM: SS format

1

Hello, is there a possibility of a chronometer displaying more than 60 minutes without entering the time format?

My chronometer receives data from a String to set the initial value.

private long minutesAndSecondsToMilliseconds(String minutesAndSeconds){
    String[] timeParts = minutesAndSeconds.split(":");
    int minutes = Integer.parseInt(timeParts[0]);
    int seconds = Integer.parseInt(timeParts[1]);
    return (minutes * 60 + seconds) * 1000;
}

But on receiving 80:00 minutes for example, the chronometer displays 1:20:00 . Is it possible to display 80:00 ?

    
asked by anonymous 18.03.2018 / 02:38

1 answer

1

By default, the value is displayed in "MM: SS" format, changing to "H: MM: SS" when the minute is greater than 59.

The Chronometer class provides the serFormat () However, contrary to what your name may suggest, it is not meant to change this standard. It serves to compose a string, to be displayed, where a first occurrence of "% s" is replaced by the current stopwatch value, in the form "MM: SS" or "H: MM: SS".

For example, using chronometer.setFormat("Passaram %s desde o início"); will result in

  • if the value is less than 60 seconds:

    Passaram 48:55 desde o início

  • if the value is greater than 59 seconds:

    Passaram 1:25:23 desde o início

If there is no method in the class that does this directly, you can do it indirectly using a OnChronometerTickListener and in the onChronometerTick() method, take the current value, format it as you want, and assign it to the text of the Chronometer.

chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer chronometer) {
        long time = SystemClock.elapsedRealtime() - chronometer.getBase();
        String minSec = String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(time),
                TimeUnit.MILLISECONDS.toSeconds(time) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time))
        );
        chronometer.setText(minSec);
    }
});
    
18.03.2018 / 16:52