![]() |
VOOZH | about |
Android ChronoMeter is user interface control which shows timer in the view. We can easily start up or down counter with base time using the chronometer widget. By default, start() method can assume base time and starts the counter.
Generally, we can create use ChronoMeter widget in XML layout but we can do it programmatically also.
First we create a new project by following the below steps:
In this file, we use the ChronoMeter widget along with a button to start or stop the meter and also set attributes for both of them.
Note:android:countDown is a attribute which is used to define whether the chronometer will count up or count down.
activity_main.xml:
Here, we update the name of the application using the string tag. We also other strings which can be used in MainActivity.kt file.
MainActivity.kt:
First, we declare a variable meter to access the Chronometer from the XML layout file.
val meter = findViewById<Chronometer>(R.id.c_meter)then, we access the button from the xml file and set setOnClickListener to start and stop the timer.
val btn = findViewById<Button>(R.id.btn)
btn?.setOnClickListener(object : View.OnClickListener {...}MainActivity.kt: