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:
- TextClock in Kotlin
- Dynamic Button in Kotlin
- Dynamic ScrollView in Kotlin
- Dynamic RadioGroup in Kotlin
- Dynamic DatePicker in Kotlin
- Dynamic TimePicker in Kotlin
- Dynamic TextView in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic Chronometer in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic ImageSwitcher in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic EditText in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.