Android ChronoMeter Using Kotlin With Example

Hello Readers! In this post, we are going to learn about android chronoMeter using kotlin in any android application. We will also learn about different attributes of android chronoMeter that can be used to customise this widget.


Tutorialwing Kotlin ChronoMeter Output Android ChronoMeter using kotlin Tutorial With example

Tutorialwing Kotlin ChronoMeter Output

Getting Started

Android ChronoMeter can be defined as below –

ChronoMeter is simple widget that can be used as simple timer. You can count up or down with a given base time using chronoMeter. If you do not provide a base time, it will assume base time as time at which you call start() method.

Different Attributes of Android ChronoMeter Widget

Some of the popular attributes of android ChronoMeter widget are –

Sr. XML Attributes Description
1 android:countDown Defines whether this chronometer will count up or count down from the base value
2 android:format Defines the format of the string to be shown

Some of the popular attributes of android chronoMeter inherited from TextView are –

Sr. XML Attributes Description
1 android:gravity Defines gravity of the view
2 android:height Defines height of the view
3 android:maxHeight Defines maximum height of the view
4 android:maxLength Set an input filter to constrain the text length to the specified number
5 android:maxWidth Defines maximum width of the view
6 android:minHeight Defines minimum height of the view
7 android:minWidth Defines minimum width of the view
8 android:width Defines width of the view

Some of the popular attributes of android chronoMeter inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines alpha of the view
2 android:background Defines drawable of the background of the view
3 android:elevation Defines elevation of the view
4 android:id Defines id of the view
5 android:padding Defines padding to apply the edges of the view
6 android:visibility Defines visibility(VISIBLE, INVISIBLE or GONE) of the view

Example of Android ChronoMeter Using Kotlin

At first, we will create android application. Then, we will use chronoMeter using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ChronoMeter. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use chronoMeter using kotlin in the application.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">Chronometer</string>
	<string name="stop">Stop</string>
	<string name="start">Start</string>
	<string name="playing">Playing...</string>
	<string name="stopped">Stopped...</string>

3. Use ChronoMeter Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>




In activity_main.xml file, we have defined chronoMeter and button widgets. Now, we will access this widgets in kotlin file to perform some operations on it.

4. Access ChronoMeter Widget in Kotlin file

Open src/main/java/com.tutorialwing.chronometer/MainActivity.kt file and add below code into it.

package com.tutorialwing.chronometer

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Chronometer
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val chronometer = findViewById<Chronometer>(

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

            internal var isPlaying = false

            override fun onClick(v: View) {
                if (!isPlaying) {
                    isPlaying = true
                } else {
                    isPlaying = false

                button.setText(if (isPlaying) R.string.start else R.string.stop)
                Toast.makeText(this@MainActivity, getString(if (isPlaying) R.string.playing else R.string.stopped), Toast.LENGTH_SHORT).show()

We have accessed button and chronoMeter using kotlin file (i.e. MainActivity.kt file) in the application. Then, we have set a click listener to play/pause chronoMeter. We are also showing message, as toast message, that represents the current status of the chronoMeter.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.chronometer"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android ChronoMeter Using Kotlin.