Simple CountDown Timer in android.

By | August 24, 2011

Here is a simple example on using a countdown timer in android.

package pack.coderzheaven;

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 TimerDemo extends Activity {
	Button start, stop;
	TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        start = (Button)findViewById(R.id.start);
        stop = (Button)findViewById(R.id.stop);
        tv  = (TextView)findViewById(R.id.tv);
        tv.setText("10"); // startting from 10.

        final MyCounter timer = new MyCounter(10000,1000);
        start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				timer.start();
			}
		});
        stop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				timer.cancel();
			}
		});
    }

    public class MyCounter extends CountDownTimer{

    	public MyCounter(long millisInFuture, long countDownInterval) {
    		super(millisInFuture, countDownInterval);
    	}

		@Override
		public void onFinish() {
			System.out.println("Timer Completed.");
			tv.setText("Timer Completed.");
		}

		@Override
		public void onTick(long millisUntilFinished) {
			tv.setText((millisUntilFinished/1000)+"");
			System.out.println("Timer  : " + (millisUntilFinished/1000));
		}
    }
}

Here is the Layout XML(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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="CountDown Timer Demo"
    />
<Button
	android:text="Start Timer"
	android:id="@+id/start"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
<Button
	android:text="Stop Timer"
	android:id="@+id/stop"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
<TextView
	android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    />
</LinearLayout>
Timer Demo

Timer Demo

5 thoughts on “Simple CountDown Timer in android.

  1. Pingback: Pass the name of button clicked to different class in Android : Android Community - For Application Development

  2. Pingback: how to stop chronometer after particular time.? : Android Community - For Application Development

  3. INeed Help

    I used your code line for line, except I have an activity_main instead of a main..newer version I guess. When I click either of buttons, nothing happens. The text does not change to 10.

    Reply

Leave a Reply to James Cancel reply

Your email address will not be published. Required fields are marked *