How to Build a Basic Stopwatch App in Android Studio
16, Apr 2025
How to Build a Basic Stopwatch App in Android Studio

 

 

In this tutorial, we’ll create a simple Stopwatch Android App. This app will display the elapsed time and allow the user to Start, Stop, and Reset the stopwatch.

 

 

App UI Components

 

How to Build a Basic Stopwatch App in Android Studio

 

The stopwatch interface will include:

 

  1. TextView: Displays the elapsed time in the format `HH:MM:SS`.
  2. Start Button: Begins the stopwatch.
  3. Stop Button: Pauses the stopwatch.
  4. Reset Button: Resets the time to `00:00:00`.

 

 

Step-by-Step Guide to Build the Stopwatch App

 

Step 1: Create a New Android Project

 

  1. Open Android Studio and create a new project named Stopwatch.
  2. Use the company domain `rpssource.com`, resulting in the package name:
    `com.rpssource.stopwatch`.
  3. Set the Minimum SDK to API 14 to support most devices.
  4. Choose Empty Activity and name it `StopwatchActivity`.
  5. The default layout will be `activity_stopwatch.xml`.

 

 

Step 2: Modify AndroidManifest.xml

 

Add the following entry inside the `<application>` tag to register your activity:

 

xml
<activity android:name=”.StopwatchActivity”></activity>

 

Step 3: Define String Resources

 

Add button labels in the `res/values/strings.xml` file:

 

xml
<string name=”app_name”>Stopwatch</string>
<string name=”start”>Start</string>
<string name=”stop”>Stop</string>
<string name=”reset”>Reset</string>

 

 

Step 4: Design the Stopwatch Layout

 

Create a layout with a `TextView` and three `Buttons`. Set the `onClick` attributes directly in XML to bind each button with its respective method:

 

xml
<TextView
android:id=”@+id/time_view”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”00:00:00″
android:textSize=”40sp”
android:layout_gravity=”center” />

<Button
android:text=”@string/start”
android:onClick=”onClickStart” />

<Button
android:text=”@string/stop”
android:onClick=”onClickStop” />

<Button
android:text=”@string/reset”
android:onClick=”onClickReset” />

 

How the Stopwatch Works

 

The stopwatch behavior is controlled using two variables:

 

  1. `int seconds`: tracks total seconds passed.
  2. `boolean running`: indicates if the stopwatch is running.
  3. We’ll write the logic in the activity methods:
  4. `onClickStart()`: Starts the stopwatch.
  5. `onClickStop()`: Stops the stopwatch.
  6. `onClickReset()`: Resets the stopwatch.

 

 

 

The runTimer() Method

 

This method updates the time every second. It does the following:

 

  1. Formats the time into `HH:MM:SS`.
  2. Displays the time in the `TextView`.
  3. Increments the `seconds` count if `running` is true.
  4. Schedules itself to run again after 1 second.

 

We’ll use Android’s Handler and Runnable classes to schedule the recurring task.

 

 

Using Handler and Runnable

 

  1. Handler allows us to run code on the main thread or schedule it for later.
  2. Runnable wraps the logic to execute.
  3. Use `post()` for immediate execution.
  4. Use `postDelayed()` to schedule future execution.

 

Example:

 

 

Final Notes

 

  1. This app demonstrates key Android development concepts:
  2. UI interaction via `onClick`.
  3. Scheduled background tasks with `Handler`.
  4. Efficiently updating UI components.

>How to Create a New Project in Android Studio
>How to Run Your First Android App in Android Studio
>How to Install And Set Up Android Studio on Windows | Step-by-Step Guide

 

You now have a fully functional Stopwatch app that tracks time and provides Start, Stop, and Reset functionality.

Want help implementing the full Java code and layout? I can provide the complete working files too—just say the word!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

How to Build a Simple Calculator App Using Android Studio (Java)

  Creating a calculator app is a great beginner-friendly project to get hands-on experience with Android app development. In this…

How to Build a Video Player in Android Studio with Dialog Transition Between Two Videos

    In this tutorial, we’ll learn how to create a basic Video Player app in Android Studio that plays…

How to Install And Set Up Android Studio on Windows | Step-by-Step Guide

    If you’re beginning your journey in Android app development, the first tool you’ll need is Android Studio—the official…