Android TextClock Tutorial 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 how to use android textClock widget in any android application. We will also go through different attributes of textClock widget that can be used to customise it.

Output

Tutorialwing Android TextClock Output Android TextClock Tutorial With Example

Tutorialwing Android TextClock Output

Getting Started

TextClock widget can be defined as below –

TextClock is widget that displays current date/time in specified formatted string.

TextClock supports two format.
a. 12 hour format – The time will be shown in 12 hour format.
b. 24 hour format. The time will be shown in 24 hour format.

24 hour format is default format of textClock widget.

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 Specifies whether email or mobile number is automatically detected from the text and converted to clickable links.
2 android:drawableTint Specifies tint to apply to the compound drawables
3 android:gravity Specifies the gravity of the view
4 android:height Specifies the height of the view
5 android:maxHeight Specifies maximum height of the view
6 android:minHeight Specifies minimum height of the view
7 android:maxWidth Specifies maximum width of the view
8 android:minWidth Specifies minimum width of the view

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

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



Example of Android TextClock Widget

At first, we will create android application. Then, we will use textClock widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TextClock. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use textClock widget in the application.

2. Modify Values folder

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

3. Use TextClock Widget in xml file

Open res/layout/activity_main.xml file. Then, 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=”hh:mm:ss a” is being used provide the format of the time to show in 12 hour format.

4. Access TextClock Widget in java file

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

package com.tutorialwing.textclock;

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

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

We have not changed anything in java file. So, Above is the default java file you will get after creating new project.

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

AndroidManifest.xml

Code inside src/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 tutorial on Android TextClock widget.

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