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.
Update strings.xml file
Here, we update the name of the application using the string tag.
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)
Run as Emulator:
Please Login to comment...