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:

  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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">
  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" />
  
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="100dp"
        android:text="Show Time" />
  
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn"
        android:layout_marginLeft="85dp"
        android:layout_marginTop="20dp"
        android:textSize="15dp"
        android:textStyle="bold"
        android:textColor="#ff0012"/>
</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">DynamicTextClockInKotlin</string>
</resources>

chevron_right


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)
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.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextClock
import android.widget.TextView
  
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        //create TextClock programmatically
        val textClock = TextClock(this)
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.setMargins(140, 80, 80, 80)
        textClock.layoutParams = layoutParams
        textClock.format12Hour = "hh:mm:ss a"
  
  
        val linearLayout = findViewById<LinearLayout>(R.id.linear_layout)
        //add textClock in Linear Layout
        linearLayout?.addView(textClock)
  
        val txtView = findViewById<TextView>(R.id.textview)
  
        val btn = findViewById<Button>(R.id.btn)
        // display time when click the button
        btn?.setOnClickListener {
            txtView?.text = "Time: " + textClock?.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.