Monday 16 July 2018

Android Count Down Timer Tutorial

Hello, count down timer is a commonly used feature in interaction design or system feedback. This tutorial will show how to use the Android CountDownTimer class.

Step 1: Create a boolean (for indicate that the timer is running or not)

Step 2: Create two Constant (example startTime = 60000 for 60 seconds, interval = 1000)

Step 3: Create a inner class in the Activity/Class

public class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            //to stop the timer
            countDownTimer.cancel();
            //boolean that to indicate that the time is running or not
            isRunning = false;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //create a text view to display the count down time
            tvCountDown.setText(String.valueOf(millisUntilFinished / 1000));
        }

    }

Step 4: Create the CountDownTimer variable and initialise it like this new MyCountDownTimer(startTime, interval)

Step 5: You can start the timer with this code line: countDownTimer.start(); and remember to set the boolean isRunning to true.


No comments:

Post a Comment