3

I need chronometer without view. I tried with

chronometer = new Chronometer(getActivity());
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();

If I try to fetch chronometer value from another button

Log.e("time",""+chronometer.getText());

it always gives 00:00.

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
praj
  • 849
  • 1
  • 16
  • 39

1 Answers1

2

Chronometer doesn't store the elapsed time and it calculates it every time it needs to update the display.

You will need to add this

Log.e("time",""+SystemClock.elapsedRealtime() - chronometer.getBase());

to get the elapsed time.

To get the time formated as hh:mm:ss you can use this

millisec = SystemClock.elapsedRealtime() - chronometer.getBase();
Log.e("time",String.format("%02d min, %02d sec", 
                 TimeUnit.MILLISECONDS.toMinutes(millisec),
                 TimeUnit.MILLISECONDS.toSeconds(millisec) - 
                 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisec))
                           )
     );
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43