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.

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)this.findViewById(R.id.secondsSpinner);
        statusTextView = (TextView)this.findViewById(R.id.timerView);

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

        //the button that will start the timer
        startTimerButton = (Button)this.findViewById(R.id.startButton);
        startTimerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(timerValueSpinner.getSelectedItemPosition() > -1) {
                    int parsedSpinnerValue = 0;
                    parsedSpinnerValue = Integer.parseInt(
                            timeValues[timerValueSpinner.getSelectedItemPosition()]);

                    if (parsedSpinnerValue > 0) {
                        //if there is a timer already, end that before making a new one
                        if(timer != null) {
                            timer.cancel();
                        }

                        //initialize a new timer instance
                        timer = new SimpleCountdownTimer(parsedSpinnerValue
                                * SimpleCountdownTimer.oneSecond,
                                SimpleCountdownTimer.oneSecond,
                                statusTextView);

                        timer.start();
                    }
                }
            }
        });

        //the button that will stop the timer
        stopTimerButton = (Button)this.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.

This entry was posted in Android, Java and tagged , , , . Bookmark the permalink.