12

Let's say I have this variable:

long myMillis = 20000;

This means that I want my Chronometer to start at exactly 20 seconds (00:20).

I tried doing this:

chronometer.setBase(myMillis);

But it doesn't work. It dosn't start with 20 seconds. It starts with some weird time that doesn't make sense.

Guy
  • 6,414
  • 19
  • 66
  • 136
  • Possible duplicate of [Android: Chronometer SetBase in minutes](http://stackoverflow.com/questions/14788242/android-chronometer-setbase-in-minutes) – giannisf Jun 30 '16 at 21:49

3 Answers3

6

In general the chronometer works like this (if you would like to set the Base to a specific nr):

mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))

so make it:

 mChronometer.setBase(SystemClock.elapsedRealtime() - (2* 60000 + 0 * 1000)))
giannisf
  • 2,479
  • 1
  • 17
  • 29
  • 2
    Thank you! It worked. But I have no idea why it has to be this complicated. If anyone has an explanation, that would be great. – Guy Jun 30 '16 at 22:01
  • @Whiz You can read the official documentation https://developer.android.com/reference/android/widget/Chronometer.html – giannisf Jun 30 '16 at 22:02
6

For Kotlin,

To start Chronometer with starting time 20 seconds, you can use

val timeInMilSeconds = 20000
chronometer.base = SystemClock.elapsedRealtime() - timeInMilSeconds
chronometer.start()

This will start Chronometer with starting time 20 seconds i.e. 00:00:20

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

Its Late but may help others. I have used following code in first fragment

chronometerTimer.setBase(SystemClock.elapsedRealtime());
chronometerTimer.start();

and then move on some condition to next fragment where chornometer should start at same time of previous chornometer ends, i get elapsed time using this code.

long elapsedMillis = SystemClock.elapsedRealtime() - chronometerTimer.getBase();

and i send elapsedMilis in next fragment and use following code

chronometerTimer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
chronometerTimer.start();

it worked perfectly.

Guy
  • 6,414
  • 19
  • 66
  • 136
Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22