Android countdown

Katty picture Katty · Apr 4, 2012 · Viewed 10.1k times · Source

I want to write a countdown in android which starts counting from 3 to 0. Like at first 3 in appearing and then disappearing and 2 is appearing and so on. I searched a lot but I couldn't find any good sample. Can you help me that what should I do?

Answer

user1135750 picture user1135750 · Apr 4, 2012

use CountDownTimer

For example:

import android.os.CountDownTimer;   

MyCount timerCount;
public class TestCountdown extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    timerCount = new MyCount(3 * 1000, 1000);
    timerCount.start();
  }

  public class MyCount extends CountDownTimer {
      public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
      }

      @Override
      public void onFinish() {
        //some script here
      }

      @Override
      public void onTick(long millisUntilFinished) {
        //some script here 
      }   
    } 
}