Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a simple countdown timer in Kotlin?

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?

like image 882
Andy Jazz Avatar asked Jan 08 '19 16:01

Andy Jazz


People also ask

How do you use the ChronoMeter on 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.

What is CountDownTimer?

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.


1 Answers

You can use Kotlin objects:

val timer = object: CountDownTimer(20000, 1000) {     override fun onTick(millisUntilFinished: Long) {...}      override fun onFinish() {...} } timer.start() 
like image 147
Danail Alexiev Avatar answered Oct 04 '22 09:10

Danail Alexiev