Here you will get android stopwatch example.
In this android tutorial I will guide you to make a simple stopwatch example using Chronometer.
In android we can use Chronometer class to display a simple timer.
Also Read: Simple Notepad App Android Example
Android Stopwatch Example Using Chronometer
Create a new android project with package name com.androidstopwatch
Now add following code in respective files.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context="com.androidstopwatch.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Watch"
android:textSize="20dp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="@+id/chronometer"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_marginBottom="10dp"
android:id="@+id/startBtn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_marginBottom="10dp"
android:visibility="gone"
android:id="@+id/pauseBtn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/resetBtn"
android:layout_marginBottom="10dp"/>
</LinearLayout>
MainActivity.java
package com.androidstopwatch;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
Chronometer chronometer;
Button startBtn, pauseBtn, resetBtn;
long stopTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = (Chronometer)findViewById(R.id.chronometer);
startBtn = (Button)findViewById(R.id.startBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
resetBtn = (Button)findViewById(R.id.resetBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime() + stopTime);
chronometer.start();
startBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopTime = chronometer.getBase() - SystemClock.elapsedRealtime();
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
resetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
stopTime = 0;
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
}
}
Save the project and run the app.
Screenshot

The code in above example is self explanatory, if still you are facing any problem then feel free to ask in comment section.
