Wednesday, May 2, 2012

Stop Watch Example


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:
<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">
    <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"
       />
    <Button android:id="@+id/start"
       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>
    <Button android:id="@+id/reset"
       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>
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.
Share and Enjoy:

1 comment:

Anonymous said...

I'm not certain the place you are getting your info, but good topic. I needs to spend a while finding out much more or figuring out more. Thanks for wonderful information I used to be searching for this info for my mission.

Here is my weblog: online stopwatch for teachers.