Run as Emulator



Chronometer in Kotlin

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: 

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Similar Reads

Different attributes for ChronoMeter widget

XML attributes Description android:id Used to specify the id of the view. android:textAlignment Used to the text alignment in the dropdown list. android:background Used to set the background of the view. android:padding Used to set the padding of the view. android:visibility Used to set the visibility of the view. android:gravity Used to specify the gravity of the view like center, top, bottom, etc android:format Used to define the format of the string to be displayed. android:countDown Used to define whether the chronometer will count up or count down....

Modify activity_main.xml file

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....

Update strings.xml file

...

Access ChronoMeter in MainActivity.kt file

Here, we update the name of the application using the string tag. We also other strings which can be used in MainActivity.kt file....

AndroidManifest.xml file

...

Run as Emulator:

First, we declare a variable meter to access the Chronometer from the XML layout file....

Contact Us