Android: Using the CountDownTimer class

In this article I go over the Android SDK CountDownTimer class. It’s easy to use and gives you the ability to count for projects like a workout timer. In this case we are extending that class so that we can access the methods we need to make it work.

Here is the code on GitHub.

https://www.youtube.com/watch?v=M_FFZ5sLOyM

Here is our SimpleCountDownTimer:


public class SimpleCountdownTimer extends CountDownTimer {

    public static int oneSecond = 1000;
    TextView textViewTimeLeftDisplay;

    public SimpleCountdownTimer(long millisInFuture, long countDownInterval, TextView textViewTimeLeftDisplay) {
        super(millisInFuture, countDownInterval);
        this.textViewTimeLeftDisplay = textViewTimeLeftDisplay;
    }

    @Override
    public void onFinish() {
        textViewTimeLeftDisplay.setText("Finished");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        textViewTimeLeftDisplay.setText(String.valueOf(millisUntilFinished / oneSecond));
    }
}

The extended timer class takes in the amount of time we want to count, the counting interval, and a textview that we will send status to. We update the text view every interval as well as when we are finished. There really isn’t much to it.

Here is our main activity:


public class MainActivity extends Activity {

    Spinner timerValueSpinner;
    Button startTimerButton;
    Button stopTimerButton;
    TextView statusTextView;
    SimpleCountdownTimer timer;
    String[] timeValues;
    Resources resourcePointer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timerValueSpinner = (Spinner) findViewById(R.id.secondsSpinner);
        statusTextView = (TextView) findViewById(R.id.timerView);

        resourcePointer = getResources();
        timeValues = resourcePointer.getStringArray(R.array.seconds_list);

        // Start button setup
        startTimerButton = (Button) findViewById(R.id.startButton);
        startTimerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedPosition = timerValueSpinner.getSelectedItemPosition();
                if (selectedPosition > -1) {
                    int parsedSpinnerValue = Integer.parseInt(timeValues[selectedPosition]);
                    if (parsedSpinnerValue > 0) {
                        // Stop any existing timer
                        if (timer != null) {
                            timer.cancel();
                        }
                        // Initialize new timer instance
                        timer = new SimpleCountdownTimer(
                                parsedSpinnerValue * SimpleCountdownTimer.oneSecond,
                                SimpleCountdownTimer.oneSecond,
                                statusTextView);
                        timer.start();
                    }
                }
            }
        });

        // Stop button setup
        stopTimerButton = (Button) findViewById(R.id.stopButton);
        stopTimerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timer != null) {
                    timer.cancel();
                }
            }
        });
    }
}

In the main activity we do a lot of referencing to our interface. In the start timer button we extract a value from the spinner, convert it into an int, make sure any existing timer is stopped, and instantiate a new timer with the spinner value.