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".


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

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
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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:background="@color/colorPrimary"
- tools:context="in.amitsin6h.stopwatch.MainActivity">
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:paddingBottom="90dp">
- <TextView
- android:text="00:00:00"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/tvTimer"
- android:textSize="50dp"
- android:textStyle="bold"
- android:textColor="#ffffff"
- android:layout_marginTop="120dp"
- android:paddingBottom="50dp"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- <Button
- android:text="Start"
- android:background="#ffffff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/tvTimer"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_marginTop="41dp"
- android:id="@+id/btStart" />
- <Button
- android:text="Pause"
- android:background="#ffffff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btPause"
- android:layout_alignBaseline="@+id/btStart"
- android:layout_alignBottom="@+id/btStart"
- android:layout_centerHorizontal="true" />
- <Button
- android:text="Reset"
- android:background="#ffffff"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignTop="@+id/btPause"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:id="@+id/btReset" />
- </RelativeLayout>
- </android.support.constraint.ConstraintLayout>



Nijin NawzatPosted Apr 16, 2022, 8:53 PM
Please sir can you help me i dont understand this long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;
Sean CPosted Feb 19, 2019, 9:03 AM
Will this app continue to run in the background?
Gajendra JangidPosted Mar 10, 2018, 6:10 AM
Great article........................