The problem is this: the countdown in my TextView starts from the 4th second and on the 1st, 0 — it is not shown, although the timer is idle, as if it counts to zero. How can I get a count from 5 to 1. Can you advise other implementations of the countdown timer?

 timer = new CountDownTimer(5000, 1000) { public void onTick(long millisUntilFinished) { // присваиваю значение TextView timerText.setText("00:0" + millisUntilFinished / 1000); } public void onFinish() {} }.start(); 

    3 answers 3

    Hurried with a question. I solved the problem like this: At the input of CountDownTimer two variables:

     new CountDownTimer(long millisInFuture, long countDownInterval) {...} 

    I changed the long countDownInterval from 1000 to 1 and everything works, it counts from 4 to 0.

    • not the best solution, you call onTick every ms, it can load the stream. - Shwarz Andrei
    • Hmmm, what can you advise? - Vlad
    • Now I answer, you need to visually show from 5 to 1? - Shwarz Andrei
    • one
      Yes, I need 5 4 3 2 1. - Vlad

    The easiest way here is to slightly increase the time of the timer so that the View has time to display the element.

     timer = new CountDownTimer(5100, 1000) { public void onTick(long millisUntilFinished) { // присваиваю значение TextView timerText.setText("00:0" + millisUntilFinished / 1000); } public void onFinish() {} }.start(); 

    The countdown will be from 5 to 1 (visually) at the end there will be a slight delay of 100ms, and the onFinish () call.

    • Yes, I initially did this, but the timer behaves incomprehensibly, then it starts counting from 4 then from 5. - Vlad
    • It should not be, put the time 5999 then 100% will be 5-4-3-2-1 but at the end there will be a delay of 999ms. Is your onCLick timer bound? By the way, you have "00: 0" + millisUntilFinished / 1000 at 10 + s and will no longer be beautiful - Shwarz Andrei
    • I only need 5. - Vlad
     timer = new CountDownTimer(5000, 1000) { public void onTick(long millisUntilFinished) { // присваиваю значение TextView timerText.setText("00:0" + millisUntilFinished / 1000); if (millisUntilFinished == 1000) { timer.cancel(); } } public void onFinish() {} }.start(); 
    • I have not changed anything. - Vlad