Search code examples
javaandroidkotlincountdowntimer

CountDownTimer in Kotlin explanation


I am very very new to Kotlin and as well as Java. I have search around the web and found tutorials about using CountDownTimer in Kotlin but i still do not understand. I am trying to create an android app with a page that whenever you open it, it counts down for 60 sec to 0.

Below is what I have done, Was trying to create a charade game. The problem is that there is an error on textTimer.text, (unresolved reference), i don't see where i am wrong though. And is my Timer correct? Is there a better and easier way to do it?

I follow this tutorial. https://android--code.blogspot.com/2018/04/android-kotlin-countdowntimer-days.html

package com.example.sim.charades
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import java.util.concurrent.TimeUnit

class QuickStart : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quick_start)

        val textView = findViewById<TextView>(R.id.textView)
        val textTimer = findViewById<TextView>(R.id.textTimer)
        val arrayTerms = arrayListOf("Burger", "Pie", "Fries", "Donuts", "Chocolates", "Sweets")

        val rand = Random().nextInt(arrayTerms.count())
        textView.text = arrayTerms[rand]

        // 60 seconds (1 minute)
        val minute:Long = 60000

        // 1 day 2 hours 35 minutes 50 seconds
        val millisInFuture:Long = (minute * 1440) + (minute * 155) + (1000 * 50)

        // Count down interval 1 second
        val countDownInterval:Long = 1000

        timer(millisInFuture,countDownInterval).start()


    }

    private fun timer(millisInFuture:Long,countDownInterval:Long):CountDownTimer{
        return object: CountDownTimer(millisInFuture,countDownInterval){
            override fun onTick(millisUntilFinished: Long){
                val timeRemaining = timeString(millisUntilFinished)
                textTimer.text = timeRemaining
            }

            override fun onFinish() {
                //nothing Yet
            }
        }
    }

    private fun timeString(millisUntilFinished:Long):String{
        var millisUntilFinished:Long = millisUntilFinished

        val seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)

        // Format the string
        return String.format(
                Locale.getDefault(),
                "%02d sec",
                seconds
        )
    }


}

Thanks Guys


Solution

  • Your view is not yet inflated in the onCreate method. Move your view initialization into the onViewCreated method and you should be able to resolve your TextView reference.

    Also, using import kotlinx.android.synthetic.main.activity_main.* you don't have to use findViewById, you can directly access your view by its id. You can remove those lines (your variables seem to match the id name)