Android full project with source code to build a stopwatch that we can start, pause, resume and reset with help of the Chronometer widget. Chronometer is a subclass of TextView and automatically counts upwards when we call start on it. With setBase we define a time the Chronometer should count in relation to. If we want to start at 0 seconds, we have to pass Systemclock.elapsedRealtime. We can also easily change the format of the clock with help of the setFormat method, we can make it count downwards with setCountdown and we can implement an OnChronometerTickListener and override the onChronometerTick method to do something every second when the Chronometer updates
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="tech.codingpoint.chronometerexample.MainActivity">
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startChronometer"
android:text="Start" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseChronometer"
android:text="Pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetChronometer"
android:text="Reset" />
</LinearLayout>
MainActivity.java
package tech.codingpoint.chronometerexample;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Chronometer;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Chronometer chronometer;
private long pauseOffset;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = findViewById(R.id.chronometer);
chronometer.setFormat("Time: %s");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 10000) {
chronometer.setBase(SystemClock.elapsedRealtime());
Toast.makeText(MainActivity.this, "Bing!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void startChronometer(View v) {
if (!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}
}
public void pauseChronometer(View v) {
if (running) {
chronometer.stop();
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
}
public void resetChronometer(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
}