Stop Watch Example

This example will show how to use chronometer to include stopwatch functionality in your app.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it StopWatch.

2.) You will see some default code into your main.xml, strings.xml and android manifest file.

3.) Now add 1 buttons into your main.xml or write following into main.xml file:

 

android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

android:format="@string/chronometer_initial_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="30dip"
android:paddingTop="30dip"
/>

<button> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start">

</button>

<button> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop">
</button>

<button> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset">
</button>

 

 

4.) Your launcher activity StopWatch will have one default functions OnCreate().

5.) Write following into strings.xml.

 

<!--?xml version="1.0" encoding="utf-8"?-->

Hello World, StopWatch!
StopWatch
Initial format:%

 

 

Steps:

1.) Create a project named StopWatch and set the information as stated in the image.

Build Target: Android 1.6
Application Name: StopWatch
Package Name: com.example. StopWatch
Activity Name: StopWatch
Min SDK Version: 4

2.) Open StopWatch.java file and write following code there:

 

package com.example.StopWatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class StopWatch extends Activity {
Chronometer mChronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button button;

mChronometer = (Chronometer) findViewById(R.id.chronometer);

// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);

button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);

button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);

}

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};

View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};

View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
}

 

 

3.) Compile and build the project.

4.) Run on 1.6 simulator for the output.

Leave a Comment:

12 comments
Raghvendra Kr. Tiwari says November 4, 2011

thanks..sir.

Reply
Amit Hooda says November 7, 2011

stop is not working,,,,even though i click on stop and after sometime when i start it then the time it shows is the total time from the start the pause is not considered at all,,,

Reply
PRITU says July 25, 2012

nice source… could you plz tell what if i want to restart the timer from the time i stopped it…

Reply
PRITU says July 25, 2012

nice source… could you plz tell what if i want to restart the timer from the time i stopped it..
thanks in advance.

Reply
farhadfery says November 26, 2012

very usefull post!
thanks.

Reply
Alibaba says December 1, 2012

Hey Great Tutorial,
how can i change the format to HH:MM:SS
THX

I do not get the format declaration in the String.xml file, can you explain that

Reply
hasan zahed says January 27, 2013

Thanks a lot for this step by step how to

Reply
Diarra says February 9, 2013

cannot resolve id
Please update tutorial to present sofware

Reply
Usman says April 13, 2013

Thanksssssssssss Sir

Reply
Umair says April 20, 2013

How can i change Format ?

Reply
Diego says July 25, 2013

Hey dude, do you have one done with lap? Well I’d like to know how I can do that

Reply
Thorben says November 13, 2013

I have done everything, like it is descriebed, but it doesn´t start, at StopWatch.java it shows mistakes at the button links.

Reply
Add Your Reply