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 Comments

  1. Gnomem October 27, 2012 at 5:03 pm

    Thanks à lot.

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

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

  4. INeed Help April 4, 2013 at 5:33 pm

    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.

     
    • James April 5, 2013 at 4:54 pm

      Paste your code here then only I can check.

       

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.