Dynamic TextClock in Kotlin
Android TextClock is a user interface control which is used to show the date/time in string format.
It provides time in two modes, 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.
Here, we will create the TextClock programmatically in the Kotlin file.
First we create a new project by following the below steps:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
Modify activity_main.xml file
In this file, we use the TextView and Button and set attributes of all the widgets.
XML
<? xml version = "1.0" encoding = "utf-8" ?> <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "wrap_content" android:layout_height = "wrap_content" tools:context = ".MainActivity" > android:id = "@+id/linear_layout" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "vertical" /> < Button android:id = "@+id/btn" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "100dp" android:layout_marginTop = "100dp" android:text = "Show Time" /> < TextView android:id = "@+id/textview" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@+id/btn" android:layout_marginLeft = "85dp" android:layout_marginTop = "20dp" android:textSize = "15dp" android:textStyle = "bold" android:textColor = "#ff0012" /> </ RelativeLayout > |
Update strings.xml file
Here, we update the name of the application using the string tag.
XML
< resources > < string name = "app_name" >DynamicTextClockInKotlin</ string > </ resources > |
Create TextClock in MainActivity.kt file
First, we a variable to create TextClock like this
val textClock = TextClock(this)
then, set the format of the clock display in the layout.
textClock.format12Hour = "hh:mm:ss a"
We have to add the textClock in the linear layout using
val linearLayout = findViewById(R.id.linear_layout) //add textClock in Linear Layout linearLayout?.addView(textClock)
Kotlin
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.ViewGroup import android.widget.Button import android.widget.LinearLayout import android.widget.TextClock import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) //create TextClock programmatically val textClock = TextClock( this ) val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) layoutParams.setMargins( 140 , 80 , 80 , 80 ) textClock.layoutParams = layoutParams textClock.format12Hour = "hh:mm:ss a" val linearLayout = findViewById<LinearLayout>(R.id.linear_layout) //add textClock in Linear Layout linearLayout?.addView(textClock) val txtView = findViewById<TextView>(R.id.textview) val btn = findViewById<Button>(R.id.btn) // display time when click the button btn?.setOnClickListener { txtView?.text = "Time: " + textClock?.text } } } |
AndroidManifest.xml file
XML
<? xml version = "1.0" encoding = "utf-8" ?> package = "com.geeksforgeeks.myfirstkotlinapp" > < 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 > |
Run as Emulator:


Please Login to comment...