Android TextClock Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about android textClock using kotlin in any android application. We will also learn about different attributes of android textClock that can be used to customise this widget.

Output

Tutorialwing Kotlin TextClock Output Android TextClock Using Kotlin With Example

Tutorialwing Kotlin TextClock Output

Getting Started

Android TextClock can be defined as below –

TextClock is a widget that are used to display current date and/or time in specified format.

TextClock supports 2 formats.
a. 12 hour format
b. 24 hour format.

By default, it uses 24 hour format.

Different Attributes of Android TextClock Widget

Some of the popular attributes of android textClock widget are –

Sr. XML Attributes Description
1 android:format12Hour The format in which date and/or time will be shown in 12 hour mode
2 android:format24Hour The format in which date and/or time will be shown in 24 hour mode
3 android:timeZone Defines the timezone to be used

Some of the popular attributes of android TextClock inherited from TextClock are –

Sr. XML Attributes Description
1 android:autoLink Defines whether email or mobile number is automatically detected from the text and converted to clickable links.
2 android:drawableTint Defines tint to apply to the compound drawables
3 android:gravity Defines the gravity of the view
4 android:height Defines the height of the view
5 android:maxHeight Defines maximum height of the view
6 android:minHeight Defines minimum height of the view
7 android:maxWidth Defines maximum width of the view
8 android:minWidth Defines minimum width of the view

Some of the popular attributes of android TextClock inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines alpha of the view
2 android:clickable Defines whether this view is clickable or not
3 android:id Defines id of the view
4 android:onClick Defines action to be performed when this view is clickable
5 android:padding Defines padding to apply this view
6 android:visibility Defines visibility of the view



Example of Android TextClock Using Kotlin

At first, we will create android application. Then, we will use textClock using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TextClock. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use textClock using kotlin in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use TextClock Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical">

	<TextClock
		android:id="@+id/textClock"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="40dp"
		android:format12Hour="hh:mm:ss a"/>

</LinearLayout>

In activity_main.xml file, we have defined textClock widget. Attribute android:format12Hour is being used to specify the format for 12 hour format of the textClock.

4. Access TextClock Widget in Kotlin file

Open src/main/java/com.tutorialwing.textclock/MainActivity.kt file and add below code into it.

package com.tutorialwing.textclock

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

We have not changed anything in MainActivity.kt file.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.textclock"
		  xmlns:android="http://schemas.android.com/apk/res/android">

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

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android textClock using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners