Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
HomeF.A.Q.
Community
Writing Recipes
Login
Program: A Simple CountDownTimer example
Contributed by Wagied Davids 2011-04-12 12:54:08 (updated 2011-11-11 12:45:55)
In Published Edition? Yes
0
Votes
Problem

Require a simple count down timer

Solution

Android comes with a built-in class for constructing CountDownTimers. Its easy to use, efficient and works..(that goes without saying!).

  1. Create a subclass of CountDownTimer.
  2. Override the onTick() and onFinish() methods.
  3. Instantiate a new instance in your Android Activity,
  4. Call the start() method on the new instance created!
Discussion

File: main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button
		android:id="@+id/button"
		android:text="Start"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<TableLayout
		android:padding="10dip"
		android:layout_gravity="center"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TableRow>
			<TextView
				android:id="@+id/timer"
				android:text="Time: "
				android:paddingRight="10dip"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content" />

			<TextView
				android:id="@+id/timeElapsed"
				android:text="Time elapsed: "
				android:paddingRight="10dip"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content" />
		</TableRow>
	</TableLayout>
</LinearLayout>

File: Main.java

package com.examples;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener
	{
		private static final String tag = "Main";
		private MalibuCountDownTimer countDownTimer;
		private long timeElapsed;
		private boolean timerHasStarted = false;
		private Button startB;
		private TextView text;
		private TextView timeElapsedView;

		private final long startTime = 50000;
		private final long interval = 1000;

		/** Called when the activity is first created. */
		@Override
		public void onCreate(Bundle savedInstanceState)
			{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.main);
				startB = (Button) this.findViewById(R.id.button);
				startB.setOnClickListener(this);

				text = (TextView) this.findViewById(R.id.timer);
				timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
				countDownTimer = new MalibuCountDownTimer(startTime, interval);
				text.setText(text.getText() + String.valueOf(startTime));
			}

		@Override
		public void onClick(View v)
			{
				if (!timerHasStarted)
					{
						countDownTimer.start();
						timerHasStarted = true;
						startB.setText("Start");
					}
				else
					{

						countDownTimer.cancel();
						timerHasStarted = false;
						startB.setText("RESET");
					}
			}

		// CountDownTimer class
		public class MalibuCountDownTimer extends CountDownTimer
			{

				public MalibuCountDownTimer(long startTime, long interval)
					{
						super(startTime, interval);
					}

				@Override
				public void onFinish()
					{
						text.setText("Time's up!");
						timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
					}

				@Override
				public void onTick(long millisUntilFinished)
					{
						text.setText("Time remain:" + millisUntilFinished);
						timeElapsed = startTime - millisUntilFinished;
						timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
					}
			}
	}

The result looks like this:

Code Repository Subdirectory The code example is in the "CountDownTimerExample" subdirectory of our GitHub repo, and can be viewed at https://github.com/androidcook/Android-Cookbook-Examples/tree/master/CountDownTimerExample You can also download the complete collection of examples at https://github.com/androidcook/Android-Cookbook-Examples/.
Comments (2)
Leave a comment
Edit History (12)
dannysun said 2013-07-18 06:45:30 Glad that Android added this class to what Java missed. Actually reading the source code of CountDownTimer class is quite helpful.
vguido said 2012-08-25 09:44:09 The "@Override" above the onClick() method is a mistake. With it I get a build error. Check out http://developer.android.com/guide/topics/ui/ui-events.html, for the correct syntax. - Thanks Vinny