Android ChronoMeter Using Kotlin With Example

In this article, we will learn about android ChronoMeter using Kotlin. We will go through various example that demonstrates how to use different attributes of ChronoMeter. For example,

In this article, we will get answer to questions like –

  • What is ChronoMeter?
  • Why should we consider ChronoMeter while designing ui for any app?
  • What are possibilities using ChronoMeter while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin ChronoMeter Output Android ChronoMeter using kotlin Tutorial With example

Tutorialwing Kotlin ChronoMeter Output

Getting Started

We can define android ChronoMeter widget 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.

Now, how do we use ChronoMeter in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ChronoMeter. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ChronoMeter Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ChronoMeter in Kotlin

Follow steps below to use ChronoMeter in newly created project –

  • 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>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Chronometer
            android:id="@+id/chronoMeter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="@string/start"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/chronoMeter" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.chronometer
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import com.tutorialwing.chronometer.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupChronoMeter()
    	}
    
    	private fun setupChronoMeter() {
    		binding.btnStart.setOnClickListener(object : View.OnClickListener {
    
    			var isPlaying = false
    
    			override fun onClick(v: View) {
    				val chronoMeter = binding.chronoMeter
    				isPlaying = if (!isPlaying) {
    					chronoMeter.start()
    					true
    				} else {
    					chronoMeter.stop()
    					false
    				}
    
    				binding.btnStart.setText(if (isPlaying) R.string.stop else R.string.start)
    				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.

Now, run the application. We will get output as below –

Tutorialwing Kotlin ChronoMeter Output Android ChronoMeter using kotlin Tutorial With example

Tutorialwing Kotlin ChronoMeter Output

Different Attributes of ChronoMeter in XML

Now, we will see how to use different attributes of Android ChronoMeter using Kotlin to customise it –

Set Id of ChronoMeter

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ChronoMeter using android:id attribute like below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        />

Here, we have set id of ChronoMeter as chronoMeter_ID using android:id=”” attribute. So, if we need to reference this ChronoMeter, we need to use this id – chronoMeter_ID.
Learn to Set ID of ChronoMeter Dynamically

Set Width of ChronoMeter

We use android:layout_width=”” attribute to set width of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ChronoMeter Dynamically

Set Height of ChronoMeter

We use android:layout_height=”” attribute to set height of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ChronoMeter Dynamically

Set Padding of ChronoMeter

We use android:padding=”” attribute to set padding of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ChronoMeter using android:padding=”” attribute.
Learn to Set Padding of ChronoMeter Dynamically

Set Margin of ChronoMeter

We use android:layout_margin=”” attribute to set margin of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ChronoMeter using android:layout_margin=”” attribute.
Learn to Set Margin of ChronoMeter Dynamically

Set Background of ChronoMeter

We use android:background=”” attribute to set background of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ChronoMeter using android:background=”” attribute.
Learn to Set Background of ChronoMeter Dynamically

Set Visibility of ChronoMeter

We use android:visibility=”” attribute to set visibility of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ChronoMeter using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ChronoMeter Dynamically

Set Text of ChronoMeter

We use android:text=”” attribute to set text of ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        />

Here, we have set text (“Hello Tutorialwing”) in ChronoMeter using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of ChronoMeter Dynamically

Set Color of Text in ChronoMeter

We use android:textColor=”” attribute to set color of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textColor="#ffffff"     
        />

Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in ChronoMeter using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of ChronoMeter Dynamically

Set Gravity of ChronoMeter

We use android:gravity=”” attribute to set gravity of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:gravity="center_horizontal"
        />

Here, we have set gravity of text in ChronoMeter using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of ChronoMeter Dynamically

Set Text in Uppercase, Lowercase

If we need to show text of ChronoMeter in uppercase or lowercase etc.

Set text in uppercase

We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="true"
        />

Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in ChronoMeter.

By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="false"
        />

Above code will set Hello Tutorialwing to ChronoMeter.

How do we set text in lowercase?

Answer –

  • In xml file – write all the text in lowercase.
  • In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to ChronoMeter.

Learn to Set Text in Uppercase or Lowercase Dynamically

Set Size of Text in ChronoMeter

We use android:textSize=”” attribute to set size of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textSize="20sp"
        />

Here, we have set size of text in ChronoMeter using android:textSize=”” attribute.
Learn to Set Size of Text of ChronoMeter Dynamically

Set Style (Bold/italic) of Text in ChronoMeter

We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textStyle="bold"
        />

Here, we have set style of text in ChronoMeter using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of ChronoMeter Dynamically

Set Letter Spacing of Text in ChronoMeter

We use android:letterSpacing=”” attribute to set spacing between letters of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:letterSpacing="1"
        />

Here, we have set spacing between letters of text in ChronoMeter using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of ChronoMeter Dynamically

Set Typeface of Text in ChronoMeter

We use android:typeface=”” attribute to set typeface in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:typeface="sans"
        />

Here, we have set typeface of text in ChronoMeter using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of ChronoMeter Dynamically

Set fontFamily of Text in ChronoMeter

We use android:fontFamily=”” attribute to set fontFamily of text in ChronoMeter.
We can do it as below –

    <ChronoMeter
        android:id="@+id/chronoMeter_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:fontFamily="sans-serif"
        />

Here, we have set fontFamily (Here, sans-serif) of text in ChronoMeter using android:fontFamily=”sans-serif” attribute.

Till now, we have see how to use android ChronoMeter using Kotlin. We have also gone through different attributes of ChronoMeter to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android ChronoMeter Widget

Below are the various attributes that are used to customise android ChronoMeter Widget. However, you can check the complete list of attributes of ChronoMeter in it’s official documentation site. Here, we are going to list some of the important attributes of this 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

We have seen different attributes of ChronoMeter and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ChronoMeter, how can we use android ChronoMeter using Kotlin ? etc. We also went through different attributes of android ChronoMeter.

Leave a Reply