VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/dynamic-textclock-in-kotlin/

⇱ Dynamic TextClock in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic TextClock in Kotlin

Last Updated : 28 Mar, 2022
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:
  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.

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

AndroidManifest.xml file

Run as Emulator:

 
Comment
Article Tags:

Explore