![]() |
VOOZH | about |
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:
| XML attributes | Description |
|---|---|
| android:id | Used to specify the id of the view. |
| android:timeZone | Used to specify the zone for the time. |
| android:format12Hour | Used for the 12 hour format. |
| android:format24Hour | Used for the 24 hour format. |
| android:text | Used to specify the text. |
| android:textStyle | Used to specify the style of the text. |
| android:textSize | Used to specify the size of the text. |
| android:background | Used to set the background of the view. |
| android:padding | Used to set the padding of the view. |
| android:visibility | Used to set the visibility of the view. |
| android:gravity | Used to specify the gravity of the view like center, top, bottom, etc |
In this file, we use the TextClock, TextView, and Button and set attributes for all the widgets.
Here, we update the name of the application using the string tag.
First, we declare two variables txtClock and txtView to access the widgets from the XML layout using the id.
val txtClock = findViewById<TextClock>(R.id.txtClok) val txtView = findViewById<TextView>(R.id.textview)
then, we access the button and set OnClickListener to display the time while clicking the button.
val btn = findViewById<Button>(R.id.btn)
btn?.setOnClickListener {
txtView?.text = "Time: " + txtClock?.text