Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.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.
Output

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.
<resources> <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> </resources>
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"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp"> <Chronometer android:id="@+id/chronometer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:text="@string/start"/> </LinearLayout>
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.support.v7.app.AppCompatActivity import android.view.View import android.widget.Button import android.widget.Chronometer import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val chronometer = findViewById<Chronometer>(R.id.chronometer) val button = findViewById<Button>(R.id.button) button?.setOnClickListener(object : View.OnClickListener { internal var isPlaying = false override fun onClick(v: View) { if (!isPlaying) { chronometer.start() isPlaying = true } else { chronometer.stop() 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" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of our tutorial on Android ChronoMeter Using Kotlin.