I have a chronometer and I would like it to start counting from when the user clicks a button and not from the start of the activity. Currently, when the user clicks the button to start the timer from 0, it just shows the time from the activity started. Here is the relevant code:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startTimer: {
Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);
startTime = SystemClock.elapsedRealtime();
timer = (TextView) findViewById(R.id.timerText);
stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (countUp / 60) + ":" + (String.format("%02d", (countUp % 60)));
timer.setText(asText);
}
});
stopWatch.start();
break;
}
...
What do I need to change?
Not sure what arg0.getBase is, but I think you want to change —
to
because startTime is the time the button was clicked.