Apps

Use Android's chronometer timer widget for your apps

Look at this demonstration of Android's chronometer widget, and see if it's the right tool for the job.

One thing all computing platforms have in common are timers. In fact, every compute platform I've worked on has at least half a dozen kinds of timers. Android is no exception. Android supports Java and system timing mechanisms; additionally, it even has a UI widget called chronometer that can act as a timer for your applications.

This tutorial demonstrates Android's chronometer widget. You can follow along with the step-by-step guide or download and import the entire project directly into Eclipse.

1. Start a new Android project in Eclipse. Target Android 1.6 (Cupcake) or higher. Be sure to rename the startup activity Main.java and the corresponding layout main.xml.

2. In the /res folder, modify main.xml. We will create a simple linear layout with some horizontally nested buttons and the chronometer widget.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer Demo"
        android:textSize="20sp"/>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
                   android:orientation="horizontal"
            android:gravity="center">
            <Button
               android:text="START"
               android:id="@+id/start_button"
               android:layout_width="wrap_content"
                          android:layout_height="wrap_content"/>
              <Button
                android:text="STOP"
                android:id="@+id/stop_button"
                android:layout_width="wrap_content"
                           android:layout_height="wrap_content"/>
      </LinearLayout>
    <Chronometer
        android:id="@+id/chronometer"
        android:format="%s"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>
</LinearLayout>

3. Now we can move to the /src folder and the Main.java file. In the on create override, we initialize the chronometer and wire up the buttons. The on click callback starts or stops the chronometer depending on which button was pressed. Simple!

Main.java

package com.authorwjf.stopwatch;
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;
public class Main extends Activity implements OnClickListener {
       private Chronometer chronometer;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             chronometer = (Chronometer) findViewById(R.id.chronometer);
             ((Button) findViewById(R.id.start_button)).setOnClickListener(this);
             ((Button) findViewById(R.id.stop_button)).setOnClickListener(this);
       }
       @Override
       public void onClick(View v) {
              switch(v.getId()) {
              case R.id.start_button:
                     chronometer.setBase(SystemClock.elapsedRealtime());
                     chronometer.start();
                     break;
             case R.id.stop_button:
                    chronometer.stop();
                    break;
             }
      }
}
We are ready to load the APK to a device or simulator. You can see the widget in action by clicking the Start button (Figure A). Figure A

Note that the accuracy of the widget is to the second. This is the finest resolution supported by the chronometer, so if you are looking for sub-second timing related to data, the chronometer is not the right tool for the job. That said, there are many cases where sub-second timing data is not needed, and the chronometer is a reliable, easy-to-use widget available on almost every version of Android.

About

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

0 comments

Editor's Picks