Android App to Create a Digital Stopwatch Clock Using Java & XML Using Android Studio for Beginners
strings.xml
<resources>
<string name="app_name">GFG|Stopwatch</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="reset">Reset</string>
</resources>
XML Code
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:orientation="vertical"
<!-- Add some background color -->
android:background="#0F9D58"
android:padding="16dp"
tools:context="org.geeksforgeeks.stopwatch.StopwatchActivity">
<TextView
<!-- We will use text view "time_view" -->
<!-- to display the number of seconds. -->
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
<!-- These below attributes make the stopwatch -->
<!-- timer nice and big. -->
android:textAppearance="@android:style/TextAppearance.Large"
android:textSize="56sp" />
<Button
<!-- This code is for the Start button.
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
<!-- When it gets clicked, the Start button -->
<!-- calls the onClickStart() method. -->
android:onClick="onClickStart"
android:text="@string/start" />
<Button
<!-- This code is for the Stop button. -->
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
<!-- When it gets clicked, the Stop button -->
<!-- calls the onClickStop() method. -->
android:onClick="onClickStop"
android:text="@string/stop" />
<Button
<!-- This code is for Reset button. -->
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
<!-- When it gets clicked, the Reset button -->
<!-- calls the onClickReset() method. -->
android:onClick="onClickReset"
android:text="@string/reset" />
</LinearLayout>
Java Code
activity_main.xml
package org.geeksforgeeks.stopwatch;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.os.Bundle;
import java.util.Locale;
import android.widget.TextView;
public class StopwatchActivity extends Activity {
// Use seconds, running and wasRunning respectively
// to record the number of seconds passed,
// whether the stopwatch is running and
// whether the stopwatch was running
// before the activity was paused.
// Number of seconds displayed
// on the stopwatch.
private int seconds = 0;
// Is the stopwatch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null) {
// Get the previous state of the stopwatch
// if the activity has been
// destroyed and recreated.
seconds
= savedInstanceState
.getInt("seconds");
running
= savedInstanceState
.getBoolean("running");
wasRunning
= savedInstanceState
.getBoolean("wasRunning");
}
runTimer();
}
// Save the state of the stopwatch
// if it's about to be destroyed.
@Override
public void onSaveInstanceState(
Bundle savedInstanceState)
{
savedInstanceState
.putInt("seconds", seconds);
savedInstanceState
.putBoolean("running", running);
savedInstanceState
.putBoolean("wasRunning", wasRunning);
}
// If the activity is paused,
// stop the stopwatch.
@Override
protected void onPause()
{
super.onPause();
wasRunning = running;
running = false;
}
// If the activity is resumed,
// start the stopwatch
// again if it was running previously.
@Override
protected void onResume()
{
super.onResume();
if (wasRunning) {
running = true;
}
}
// Start the stopwatch running
// when the Start button is clicked.
// Below method gets called
// when the Start button is clicked.
public void onClickStart(View view)
{
running = true;
}
// Stop the stopwatch running
// when the Stop button is clicked.
// Below method gets called
// when the Stop button is clicked.
public void onClickStop(View view)
{
running = false;
}
// Reset the stopwatch when
// the Reset button is clicked.
// Below method gets called
// when the Reset button is clicked.
public void onClickReset(View view)
{
running = false;
seconds = 0;
}
// Sets the NUmber of seconds on the timer.
// The runTimer() method uses a Handler
// to increment the seconds and
// update the text view.
private void runTimer()
{
// Get the text view.
final TextView timeView
= (TextView)findViewById(
R.id.time_view);
// Creates a new Handler
final Handler handler
= new Handler();
// Call the post() method,
// passing in a new Runnable.
// The post() method processes
// code without a delay,
// so the code in the Runnable
// will run almost immediately.
handler.post(new Runnable() {
@Override
public void run()
{
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
// Format the seconds into hours, minutes,
// and seconds.
String time
= String
.format(Locale.getDefault(),
"%d:%02d:%02d", hours,
minutes, secs);
// Set the text view text.
timeView.setText(time);
// If running is true, increment the
// seconds variable.
if (running) {
seconds++;
}
// Post the code again
// with a delay of 1 second.
handler.postDelayed(this, 1000);
}
});
}
}