Text Clock Widget Example

By using TextClock user will be able to display the current date and time as a formatted string using android.widget.TextClock, introduced in API level 17.

This will follow the 24-hour format system. In that capacity, there are possibilities and recommendation to give two different formatting examples: one to show the date/time in 24-hour mode and one to show the date/time in 12-hour mode. Most users will need to use the defaults, which will be fitting for the user’s locale.

It is possible to figure out if the system is at present in 24-hour mode by calling is24hourmodeenabled().

The principles used by this widget to choose how to decide the date and time are the accompanying:

1.) In 24-hour mode:

– Utilize the value returned by getformat24hour() when valid.

– OR, utilize the value returned by getformat12hour() when valid.

– OR, utilize a default value suitable for the user’s region, for example, h:mm.

2.) In 12-hour mode:

– Utilize the value returned by getformat12hour() when valid.

– OR, utilize the value returned by getformat24hour() valid.

– OR, utilize a default value fitting for the user’s locale, for example, Hh:mm.

The Charsequence occasions utilized as formatting patterns when calling either setformat24hour(charsequence) or setformat12hour(charsequence) can contain styling data. To do along these lines, utilize a Spanned object. Note that in the event that you alter these strings, it is your obligation to supply strings proper for formatting dates and/or times in the user’s locale.

In this tutorial we will see how we can create a clock widget which will show time as text in your application/widget. You can explore more methods and field details of textclock api on developer’s site.

Below are the steps to create simple textclock example:

1.) Create a new android project in your environment.

2.) Modify main.xml and write below:

 

<LinearLayout 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:orientation="vertical"
tools:context="com.example.textclock.MainActivity" >

<TextClock
android:id="@+id/textclock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="60sp" />

</LinearLayout>

 

 

OutPut:

TextClock

Previous post in this category Stop Watch Example.

Leave a Comment: