Search code examples
androidobjectkotlintimercountdowntimer

Why CountDownTimer doesn't let lower intervals than 1s?


I'm trying to use CountDownTimer object but it doesn't let me to change countDownInternval. If I try to set it to "500", timer works in the same way as if I use "1000". Why is that? It should be twice faster.

object : CountDownTimer(30000, 500) {

                override fun onTick(millisUntilFinished: Long) {

                    TextView.text = (millisUntilFinished / 1000).toString() + ""
                }

                override fun onFinish() {
                   TextView.text = ("done!")
                }
            }.start()

Solution

  • If you use this code,

    millisUntilFinished / 1000
    

    then the TextView can only take integer values, since you are using integer division. It is updating roughly twice a second, but it's putting the same integer in there twice in a row, like this:

    29
    29
    28
    28
    27
    27
    

    ...and so on. I recommend you try either this:

    millisUntilFinished
    

    or this:

    millisUntilFinished / 1000.0f
    

    so that you can see each update, and gain a greater appreciation for the practical accuracy of a CountDownTimer.