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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Chronometer android:id="@+id/chronometer"
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"
/>
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:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
<Button android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop">
</Button>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop">
</Button>
<Button android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset">
</Button>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset">
</Button>
</LinearLayout>
4.) Your launcher activity StopWatch will have one default functions OnCreate().
5.) Write following into strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="hello">Hello World, StopWatch!</string>
<string name="app_name">StopWatch</string>
<string name="chronometer_initial_format">Initial format:<xliff:gid="initial-format">%</xliff:g></string>
</resources>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="hello">Hello World, StopWatch!</string>
<string name="app_name">StopWatch</string>
<string name="chronometer_initial_format">Initial format:<xliff:gid="initial-format">%</xliff:g></string>
</resources>
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
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;
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;
Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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.start);
button.setOnClickListener(mStartListener);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
public void onClick(View v) {
mChronometer.start();
}
};
3.) Compile and build the project.
4.) Run on 1.6 simulator for the output.
More details about this article . Go go www.edumobile.org
Other resources:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ChronometerDemo.html
http://developer.android.com/reference/android/widget/Chronometer.html
http://stackoverflow.com/questions/2324701/android-chronometer-as-a-persistent-stopwatch-how-to-set-starting-time-what-i
http://android-pro.blogspot.com/2010/06/android-chronometer-timer.html
Other resources:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ChronometerDemo.html
http://developer.android.com/reference/android/widget/Chronometer.html
http://stackoverflow.com/questions/2324701/android-chronometer-as-a-persistent-stopwatch-how-to-set-starting-time-what-i
http://android-pro.blogspot.com/2010/06/android-chronometer-timer.html
0 comments
Thanks for your comment