Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get time of chronometer widget

How to get the time from a Chronometer? I tried getText, getFormat, getBase, etc, but none of them could work.

Example code snippet:

Chronometer t = (Chronometer)findViewById(R.id.toptime); long time = SystemClock.elapsedRealtime()-t.getBase(); Log.d(null,"Was: "+time); //time is not the proper time for some reason - it is a random number between 0 and 50 t.setBase(SystemClock.elapsedRealtime()); t.start(); 
like image 374
Isaac Waller Avatar asked Feb 08 '09 22:02

Isaac Waller


People also ask

How do you find the time from a chronometer?

getText(). toString() gives you the total elapsed time in seconds (my application is some kind of stop watch, so you can pause it and resume it). Hope it helps.

What is chronometer Android?

android.widget.Chronometer. Class that implements a simple timer. You can give it a start time in the SystemClock#elapsedRealtime timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start() .

How do you use the chronometer on Kotlin?

Access ChronoMeter in MainActivity. First, we declare a variable meter to access the Chronometer from the XML layout file. then, we access the button from the xml file and set setOnClickListener to start and stop the timer. val btn = findViewById<Button>(R.


1 Answers

If you look at the source of the Chronometer class, you'll see that it doesn't store the elapsed time in a field and it calculates it internally every time it needs to update the display.

However it's relatively easy to do the same in your own code:

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

This assumes that you have started your clock something like this:

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

Here's a full example:

public class ChronoExample extends Activity { Chronometer mChronometer;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      LinearLayout layout = new LinearLayout(this);     layout.setOrientation(LinearLayout.VERTICAL);      mChronometer = new Chronometer(this);     layout.addView(mChronometer);      Button startButton = new Button(this);     startButton.setText("Start");     startButton.setOnClickListener(mStartListener);     layout.addView(startButton);      Button stopButton = new Button(this);     stopButton.setText("Stop");     stopButton.setOnClickListener(mStopListener);     layout.addView(stopButton);      Button resetButton = new Button(this);     resetButton.setText("Reset");     resetButton.setOnClickListener(mResetListener);     layout.addView(resetButton);              setContentView(layout); }  private void showElapsedTime() {     long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();                 Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,              Toast.LENGTH_SHORT).show(); }  View.OnClickListener mStartListener = new OnClickListener() {     public void onClick(View v) {         mChronometer.start();         showElapsedTime();     } };  View.OnClickListener mStopListener = new OnClickListener() {     public void onClick(View v) {         mChronometer.stop();         showElapsedTime();     } };  View.OnClickListener mResetListener = new OnClickListener() {     public void onClick(View v) {         mChronometer.setBase(SystemClock.elapsedRealtime());         showElapsedTime();     } }; } 

One somewhat confusing thing about Chronometer is that you can't really use it as a stopwatch that gets started, stopped and restarted again. When it's running, it will always show the time elapsed since you last reset it, no matter how many times and for how long you have stopped it in the meantime. When it is stopped, it simply stops updating the display.

If you need something like a stopwatch you'll have to subclass Chronometer or maybe create your own version using the source.

alt text

like image 167
nyenyec Avatar answered Sep 17 '22 14:09

nyenyec