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.


