Text Clock Code Snippet for Android Studio


Welcome. In this tutorial you will learn how to insert a Clock that will show current time as text in Android Studio.

Android Text Clock Example

A text clock is an UI element that will display the current time and/or date. You can display the time in either 12 hour format or 24 hour format according to your need. You can insert a Text Clock in your application using both XML or Java file.

Inserting a Text Clock using XML

To insert a text clock, just add the following lines in your activity_main.xml file:-

The above code will create a text clock with 12 hour format. If you want to add a 24 hour format clock, then just add the line android:format24Hour in place of  android:format12Hour. You can also add background, padding, and other properties to your clock UI.

If you want to check whether 24 hour mode is being used by the system or not, then you can call this function is24HourModeEnabled(). 

Control a Text Clock Using Java

You will have to get the current time from the system. For that purpose, first create a tclock variable of type textclock as shown:

Then receive the current time by using tClock.getText().It will be received as a string and you can then use it as per your need.


An example for Text Clock is shown in the Image Below:-

Text clock Example code for Android Studio


The Full Java Code along with the XML layout for creating a similar UI is shown below:-

JAVA (MainActivity.java)


XML (activity_main.xml)



You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with various other settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your UI more interesting.

For a more detailed guide, you can also visit android’s official guide below:-


Comment down below if you are facing any problems.


Also Read:-

Leave a Reply

Your email address will not be published. Required fields are marked *