Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>However it's relatively easy to do the same in your own code:</p> <pre><code>long elapsedMillis = SystemClock.elapsedRealtime() - chronometerInstance.getBase(); </code></pre> <p>This assumes that you have started your clock something like this:</p> <pre><code>chronometerInstance.setBase(SystemClock.elapsedRealtime()); chronometerInstance.start(); </code></pre> <p>Here's a full example:</p> <pre><code>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(); } }; } </code></pre> <p>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, <em>no matter how many times and for how long you have stopped it in the meantime</em>. When it is stopped, it simply stops updating the display.</p> <p>If you need something like a stopwatch you'll have to subclass Chronometer or maybe create your own version using the <a href="http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/widget/Chronometer.java&amp;q=android%20chronometer" rel="noreferrer">source</a>.</p> <p><img src="https://farm4.static.flickr.com/3301/3273256911_192eae1767_o.png" alt="alt text"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload