Open In App
Related Articles

TextClock in Kotlin

Like Article
Save Article
Report issue

Android TextClock is a user interface control that is used to show the date/time in string format.

It provides time in two modes, the first one is to show the time in 24 Hour format and another one is to show the time in 12-hour format. We can easily use is24HourModeEnabled() method, to show the system using TextClock in 24 Hours or 12 Hours format.

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 TextClock widget

XML attributesDescription
android:idUsed to specify the id of the view.
android:timeZoneUsed to specify the zone for the time.
android:format12HourUsed for the 12 hour format.
android:format24HourUsed for the 24 hour format.
android:textUsed to specify the text.
android:textStyleUsed to specify the style of the text.
android:textSizeUsed to specify the size of the text.
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

Modify activity_main.xml file

In this file, we use the TextClock, TextView, and Button and set attributes for all the widgets. 


<?xml version="1.0" encoding="utf-8"?>
        android:format12Hour="hh:mm:ss a"
        android:text="Show Time"/>


Update strings.xml file

Here, we update the name of the application using the string tag. 


    <string name="app_name">TextClockInKotlin</string>


Access TextClock in MainActivity.kt file

First, we declare two variables txtClock and txtView to access the widgets from the XML layout using the id. 

val txtClock = findViewById<TextClock>(
        val txtView = findViewById<TextView>(

then, we access the button and set OnClickListener to display the time while clicking the button. 

val btn = findViewById<Button>(
    btn?.setOnClickListener {
    txtView?.text = "Time: " + txtClock?.text


package com.geeksforgeeks.myfirstKotlinapp
import android.os.Bundle
import android.widget.Button
import android.widget.TextClock
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val txtClock = findViewById<TextClock>(
        val txtView = findViewById<TextView>(
        val btn = findViewById<Button>(
        btn?.setOnClickListener {
            txtView?.text = "Time: " + txtClock?.text


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:




Last Updated : 27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads