Creating Stop Watch Android Application Tutorial

Introduction

 
Hello all! In this tutorial, we will learn how to create a Stopwatch Android app, which will have basic features of a stopwatch like, Start, Stop, Pause, and Reset. 
 
Requirements
  • Android Studio 2.3.3
  • Little knowledge of Java and XML
  • Android Phone to test the Android app
Let’s get started.
 
Let’s start creating the Android app using Android Studio. As usual, I have also provided the download link of the project, which you can find below.
 
Step 1 - Creating a new project with Android studio
 
Open Android Studio and create a new project, name it as "StopWatch" and give a company domain whatever you like, (you can use your own name also). 
  • Click "Next" and choose Min SDK; I have chosen Android 4.1 (Jelly Bean).
  • Click "Next" and select "Empty Activity".
  • Choose the Activity as Main Activity and click "Next", then "Finish".
     
    Android
     
    Android
Once we create our new project, Gradle will take some time to sync the project and will resolve all the dependencies. Sometimes, this process also takes a lot of time.
 
Step 2 - Creating Layout of StopWatch App 
 
Android
 
Here, we will create a layout for our app. So, for tutorial purposes, I am keeping it simple. So our StopWatch will have three buttons - Stop, Pause, and Reset, and a text view where we will display our time.
 
XML code for our layout is shown below.
 
activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:background="@color/colorPrimary"  
  8.     tools:context="in.amitsin6h.stopwatch.MainActivity">  
  9.   
  10.         <RelativeLayout  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent"  
  13.             android:layout_marginLeft="10dp"  
  14.             android:layout_marginRight="10dp"  
  15.             android:paddingBottom="90dp">  
  16.   
  17.   
  18.            <TextView  
  19.                android:text="00:00:00"  
  20.                android:layout_width="wrap_content"  
  21.                android:layout_height="wrap_content"  
  22.                android:id="@+id/tvTimer"  
  23.                android:textSize="50dp"  
  24.                android:textStyle="bold"  
  25.                android:textColor="#ffffff"  
  26.                android:layout_marginTop="120dp"  
  27.                android:paddingBottom="50dp"  
  28.                android:layout_alignParentTop="true"  
  29.                android:layout_centerHorizontal="true" />  
  30.   
  31.            <Button  
  32.                android:text="Start"  
  33.                android:background="#ffffff"  
  34.                android:layout_width="wrap_content"  
  35.                android:layout_height="wrap_content"  
  36.                android:layout_below="@+id/tvTimer"  
  37.                android:layout_alignParentLeft="true"  
  38.                android:layout_alignParentStart="true"  
  39.                android:layout_marginTop="41dp"  
  40.                android:id="@+id/btStart" />  
  41.   
  42.            <Button  
  43.                android:text="Pause"  
  44.                android:background="#ffffff"  
  45.                android:layout_width="wrap_content"  
  46.                android:layout_height="wrap_content"  
  47.                android:id="@+id/btPause"  
  48.                android:layout_alignBaseline="@+id/btStart"  
  49.                android:layout_alignBottom="@+id/btStart"  
  50.                android:layout_centerHorizontal="true" />  
  51.   
  52.            <Button  
  53.                android:text="Reset"  
  54.                android:background="#ffffff"  
  55.                android:layout_width="wrap_content"  
  56.                android:layout_height="wrap_content"  
  57.                android:layout_alignTop="@+id/btPause"  
  58.                android:layout_alignParentRight="true"  
  59.                android:layout_alignParentEnd="true"  
  60.                android:id="@+id/btReset" />  
  61.   
  62.         </RelativeLayout>  
  63.   
  64.   
  65. </android.support.constraint.ConstraintLayout>  
Step 3 – Stop Watch Android Java Code
 
Here, we will write Java code to make our app function. So for our three buttons Start, Pause and Reset we will be using OnClickListener to handle our function of buttons. Let’s see the below code to understand more clearly.
 
Just copy the below java code and paste it to MainActivity.java.
 
MainActivity.java
  1. package in.amitsin6h.stopwatch;  
  2.   
  3. import android.os.Handler;  
  4. import android.os.SystemClock;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     TextView timer ;  
  14.     Button start, pause, reset;  
  15.     long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;  
  16.     Handler handler;  
  17.     int Seconds, Minutes, MilliSeconds ;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.         timer = (TextView)findViewById(R.id.tvTimer);  
  25.         start = (Button)findViewById(R.id.btStart);  
  26.         pause = (Button)findViewById(R.id.btPause);  
  27.         reset = (Button)findViewById(R.id.btReset);  
  28.   
  29.         handler = new Handler() ;  
  30.   
  31.         start.setOnClickListener(new View.OnClickListener() {  
  32.             @Override  
  33.             public void onClick(View view) {  
  34.   
  35.                 StartTime = SystemClock.uptimeMillis();  
  36.                 handler.postDelayed(runnable, 0);  
  37.   
  38.                 reset.setEnabled(false);  
  39.   
  40.             }  
  41.         });  
  42.   
  43.         pause.setOnClickListener(new View.OnClickListener() {  
  44.             @Override  
  45.             public void onClick(View view) {  
  46.   
  47.                 TimeBuff += MillisecondTime;  
  48.   
  49.                 handler.removeCallbacks(runnable);  
  50.   
  51.                 reset.setEnabled(true);  
  52.   
  53.             }  
  54.         });  
  55.   
  56.         reset.setOnClickListener(new View.OnClickListener() {  
  57.             @Override  
  58.             public void onClick(View view) {  
  59.   
  60.                 MillisecondTime = 0L ;  
  61.                 StartTime = 0L ;  
  62.                 TimeBuff = 0L ;  
  63.                 UpdateTime = 0L ;  
  64.                 Seconds = 0 ;  
  65.                 Minutes = 0 ;  
  66.                 MilliSeconds = 0 ;  
  67.   
  68.                 timer.setText("00:00:00");  
  69.   
  70.             }  
  71.         });  
  72.   
  73.     }  
  74.   
  75.     public Runnable runnable = new Runnable() {  
  76.   
  77.         public void run() {  
  78.   
  79.             MillisecondTime = SystemClock.uptimeMillis() - StartTime;  
  80.   
  81.             UpdateTime = TimeBuff + MillisecondTime;  
  82.   
  83.             Seconds = (int) (UpdateTime / 1000);  
  84.   
  85.             Minutes = Seconds / 60;  
  86.   
  87.             Seconds = Seconds % 60;  
  88.   
  89.             MilliSeconds = (int) (UpdateTime % 1000);  
  90.   
  91.             timer.setText("" + Minutes + ":"  
  92.                     + String.format("%02d", Seconds) + ":"  
  93.                     + String.format("%03d", MilliSeconds));  
  94.   
  95.             handler.postDelayed(this, 0);  
  96.         }  
  97.   
  98.     };  
  99.   
  100. }  
If anyone faces a problem in understanding the java code, they can comment below and I will help you to understand.
 
Step 4 - Compile and Run
 
Now, we are ready to compile and run our Stopwatch Android app. The following screen will appear once our app gets installed.
 
Now, let’s test our own Android Stopwatch.
 
Android
 
Android
 
Project Source Code


Similar Articles