Ayaz Alavi Ayaz Alavi - 1 year ago 88
Android Question

Implementing counter in Android

I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show me an example of exactly how to do it?

CountDownTimer Worked. Code for showing timer for 3 seconds is.

new CountDownTimer(4000, 1000) {

public void onTick(long millisUntilFinished) {
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);
counter.setText(Long.toString(millisUntilFinished / 1000));

public void onFinish() {

Answer Source

I would better use a CountDownTimer.

If you want for example your counter to count 3 seconds:

//new Counter that counts 3000 ms with a tick each 1000 ms
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) {
    public void onTick(long millisUntilFinished) {
        //update the UI with the new count

    public void onFinish() {
        //start the activity
//start the countDown