Mobile Developer
Mobile Developer

Reputation: 191

Setting if else condition on Countdown Timer less than 10 second in Android

I am having some problem, how do i try set an if else condition if the timer is less than 10 second where it must alert user that they have 10 second remaining?

Here is the code for easier reference:

runTimer = new CountDownTimer(45000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String text = String.format(Locale.getDefault(), "%02d min: %02d sec",
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
                tvTime.setText(text);

            }

            @Override
            public void onFinish() {    
                runTimer.cancel();
            }
        }.start();

Upvotes: 0

Views: 1562

Answers (2)

DolDurma
DolDurma

Reputation: 17279

check this code:

runTimer = new CountDownTimer(45000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        String text;
        if (millisUntilFinished < 1000)
            text = String.format(Locale.getDefault(), "%02d min: %02d sec",
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
        tvTime.setText(text);
    }
    @Override
    public void onFinish() {
        runTimer.cancel();
    }
}.start();

Upvotes: 2

Mahdi Nouri
Mahdi Nouri

Reputation: 1389

try this :

runTimer = new CountDownTimer(45000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished / 1000 == 10){

                 // SHOW MESSAGE HERE

            }

        }

        @Override
        public void onFinish() {    
            runTimer.cancel();
        }
    }.start();

Upvotes: 1

Related Questions