0

Although there are answered questions about this, none REALLY answers the following: I want the android chronometer to start from the last stop, NOT to continue counting time. I try the following, but IT DOES NOT WORK :-(

public class MainActivity extends ActionBarActivity {
  long stopTime = 0;

  public void startChronometer(View view) {
    long elapsedTime = SystemClock.elapsedRealtime() - stopTime;
    ((Chronometer) findViewById(R.id.mycrono)).setBase(elapsedTime);
    ((Chronometer) findViewById(R.id.mycrono)).start();
  }
  public void stopChronometer(View view) {
    stopTime = findViewById(R.id.mycrono).getBaseline();
    ((Chronometer) findViewById(R.id.mycrono)).stop();
  }

  ............. // standard android methods

results crazy numbers...

George Violettas
  • 334
  • 1
  • 14

2 Answers2

0

i am using chronometer to start from last time l3 which is in second

chronometer.setBase(SystemClock.elapsedRealtime()
                            - (0 * 60000 + l3 * 1000));
                    chronometer.start();
vishal jangid
  • 2,967
  • 16
  • 22
0

This is works for me. I have 3 buttons (Start, pause, stop).

    private var stopTime : Long = 0


    /*Put this in your method, where you want to handle the buttons. 
      I worked with FloatingActionsButtons*/

    fab_start.setOnClickListener {
            chronometer.base = SystemClock.elapsedRealtime() + stopTime
            chronometer.start()
        }
    fab_pause.setOnClickListener {
            stopTime = chronometer.base - SystemClock.elapsedRealtime()
            chronometer.stop()
        }
    fab_stop.setOnClickListener {
            chronometer.base = SystemClock.elapsedRealtime()
            stopTime = 0
            chronometer.stop()
        }
Marci
  • 899
  • 10
  • 13