I know how to create a simple countdown timer in Java. But I'd like to create this one in Kotlin.
package android.os; new CountDownTimer(20000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { mTextField.setText("Time's finished!"); } }.start();
How can I do it using Kotlin?
Access ChronoMeter in MainActivity. First, we declare a variable meter to access the Chronometer from the XML layout file. then, we access the button from the xml file and set setOnClickListener to start and stop the timer. val btn = findViewById<Button>(R.
android.os.CountDownTimer. Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field: Kotlin Java.
You can use Kotlin objects:
val timer = object: CountDownTimer(20000, 1000) { override fun onTick(millisUntilFinished: Long) {...} override fun onFinish() {...} } timer.start()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With