Android TextClock widget example tutorial

How to use text clock widget inside android app activity to show current time.

Text clock widget is used to show current time to android application user on current opened activity. So here is the complete step by step tutorial for Android TextClock widget example tutorial.

android-project-download-code-button

Note: TextClock widget requires minimum api level 17 and dose not work below api 17. 

Android TextClock widget example tutorial.

Code for MainActivity.java file.

 package com.textclock_android_examples.com;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textclock_android_examples.com.MainActivity" >

 <TextClock
 android:id="@+id/textclock1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textSize="20dp" />
 
</RelativeLayout>

Screenshot:

Android TextClock widget example tutorial

Click here to download Android TextClock widget example tutorial project with source code.