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.

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.

Different attributes for TextClock widget

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:visibilty Used to set the visibility of the view.
android:gravity Used to specify the gravity of the view like center, top, bottom etc

Modify activity_main.xml file

In this file, we use the TextClock, TextView and Button and set attributes for all the widgets.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextClock
        android:id="@+id/txtClok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="70dp"
        android:format12Hour="hh:mm:ss a"
        android:textColor="#F1912F"
        android:textSize="30dp"
        android:textStyle="italic"/>
  
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtClok"
        android:layout_below="@+id/txtClok"
        android:layout_marginLeft="40dp"
        android:text="Show Time"/>
  
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/btn"
        android:layout_below="@+id/btn"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="-50dp"
        android:textSize="25dp"
        android:textStyle="normal"/>
</RelativeLayout>

chevron_right


Update strings.xml file

Here, we update the name of the application using the string tag.

filter_none

edit
close

play_arrow

link
brightness_4
code

<resources>
    <string name="app_name">TextClockInKotlin</string>
</resources>

chevron_right


Access TextClock in MainActivity.kt file

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
filter_none

edit
close

play_arrow

link
brightness_4
code

package com.geeksforgeeks.myfirstKotlinapp
  
import androidx.appcompat.app.AppCompatActivity
  
import android.os.Bundle
import android.widget.Button
import android.widget.TextClock
import android.widget.TextView
  
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val txtClock = findViewById<TextClock>(R.id.txtClok)
        val txtView = findViewById<TextView>(R.id.textview)
  
        val btn = findViewById<Button>(R.id.btn)
        btn?.setOnClickListener {
            txtView?.text = "Time: " + txtClock?.text
        }
    }
}

chevron_right


AndroidManifest.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


Run as Emulator:

 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.