Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Chronometer in Kotlin

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

Different attributes for ChronoMeter widget

XML attributesDescription
android:idUsed to specify the id of the view.
android:textAlignmentUsed to the text alignment in the dropdown list.
android:backgroundUsed to set the background of the view.
android:paddingUsed to set the padding of the view.
android:visibilityUsed to set the visibility of the view.
android:gravityUsed to specify the gravity of the view like center, top, bottom, etc
android:formatUsed to define the format of the string to be displayed.
android:countDownUsed 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.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintTop_toBottomOf="@+id/c_meter" />

Update strings.xml 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. 


    <string name="app_name">ChronometerInKotlin</string>
    <string name="stop">Stop Timer</string>
    <string name="start">Start Timer</string>
    <string name="working">Started</string>
    <string name="stopped">Stopped</string>

Access ChronoMeter in MainActivity.kt file

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

val meter = findViewById<Chronometer>(

then, we access the button from the xml file and set setOnClickListener to start and stop the timer. 

val btn = findViewById<Button>(
        btn?.setOnClickListener(object : View.OnClickListener {...}


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.widget.Button
import android.view.View
import android.widget.Chronometer
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // access the chronometer from XML file
        val meter = findViewById<Chronometer>(
        //access the button using id
        val btn = findViewById<Button>(
        btn?.setOnClickListener(object : View.OnClickListener {
            var isWorking = false
            override fun onClick(v: View) {
                if (!isWorking) {
                    isWorking = true
                } else {
                    isWorking = false
                btn.setText(if (isWorking) R.string.start else R.string.stop)
                Toast.makeText(this@MainActivity, getString(
                    if (isWorking)

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:

My Personal Notes arrow_drop_up
Last Updated : 28 Mar, 2022
Like Article
Save Article
Similar Reads