I have this time in seconds
( for example seconds = 2796 )
long seconds =2796;
long millis = seconds * 1000;
So I want my Chronometer to start at 46min 36sec
I tried doing this:
chronometer.setBase(myMillis);
long seconds =2796;
long stoptime_millis = seconds * 1000;
long elapsedMillis = SystemClock.elapsedRealtime() - stoptime_millis;
customChronometer.setBase(elapsedMillis);
customChronometer.start();
But it doesn't work.
with this code my chrono start in 00:00
I tried several approaches but nothing seems to help for example this one Android Chronometer start with defined value
This is my CustomChronometer
public class CustomChronometer extends Chronometer {
public int msElapsed;
public boolean isRunning = false;
public CustomChronometer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomChronometer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomChronometer(Context context) {
super(context);
}
public int getMsElapsed() {
return msElapsed;
}
public void setMsElapsed(int ms) {
setBase(getBase() - ms);
msElapsed = ms;
}
@Override
public void start() {
super.start();
setBase(SystemClock.elapsedRealtime() - msElapsed);
isRunning = true;
}
@Override
public void stop() {
super.stop();
if (isRunning) {
msElapsed = (int) (SystemClock.elapsedRealtime() - this.getBase());
}
isRunning = false;
}
}
When you call the start() method you are calling setBase again. Your msElapsed is most likely 0 so your are always reseting your Base. In this way the customChronometer.setBase(elapsedMillis); is being ignored.
So change your code like this:
@Override public void start() {
super.start();
//setBase(SystemClock.elapsedRealtime() - msElapsed); - remove this line.
isRunning = true;
}