Android TextClock

TextClock can be defined as below –

"In android, TextClock is a UI control which is used to show the current date or time as a formatted string."

TextClock supports two format.

  • 12 hour format – The time will be shown in 12 hour format.
  • 24 hour format. The time will be shown in 24 hour format.

 

24 hour format is default format of TextClock.

 

 

Some of the popular attributes of android textClock are –

S. No. XML Attributes Description
1 android:id This is unique id of the TextClock to uniquely identify the TextClock.
2 android:height Specifies the height of the view.
3 android:width Specifies the width of the view.
4 android:alpha Specifies alpha of the view.
5 android:clickable Specifies whether this view is clickable or not.
6 android:onClick Specifies action to be performed when this view is clickable.
7 android:padding Specifies padding to apply this view.
8 android:format12Hour The format in which date and/or time will be shown in 12 hour mode.
9 android:format24Hour The format in which date and/or time will be shown in 24 hour mode.
10 android:timeZone Defines the timezone to be used.
11 android:autoLink Specifies whether email or mobile number is automatically detected from the text and converted to clickable links.
12 android:drawableTint Specifies tint to apply to the compound drawables.
13 android:gravity Specifies the gravity of the view.
14 android:minHeight  Specifies minimum height of the view.
15 android:maxHeight Specifies maximum height of the view.
16 android:visibility Specifies visibility of the view.
17 android:maxWidth Specifies maximum width of the view.
18 android:minWidth Specifies minimum width of the view.

At first, we will create android application. Then, we will use TextClock in the application.

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

 

 

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project Write application name as TextClock. Then, click next button.
3 Select minimum SDK you need. However, for TextClock we need to 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 tutorial  to Create a New Project to know steps in detail.

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

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

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

<TextClock
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:format12Hour="hh:mm:ss a"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

In activity_main.xml file, we have defined TextClock. Attribute android:format12Hour=”hh:mm:ss a” is being used provide the format of the time to show in 12 hour format.

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


package com.ukacademe.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 that you will get after creating new project.

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

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

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

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