Wednesday 17 August 2016

TextClock



TextClock can display the current date and/or time as a formatted string. This view honors the 24-hour format system setting.
As such, it is possible and recommended to provide two different formatting patterns: one to display the date/time in 24-hour mode and one to display the date/time in 12-hour mode. Most callers will want to use the defaults, though, which will be appropriate for the user's locale.
Code -
<TextClock
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
/>

Java
LinearLayout layout1=(LinearLayout)findViewById(R.id.ll1);

TextClock textClock=new TextClock(this);

textClock.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

layout1.addView(textClock);


Format 12 Hour
format clock according to 12 hour and specify format for that.
Code - android:format12Hour="h:mm:ss a"

·         android:format24Hour="@null" .
·         Unset Android phone in 24 hour format ( Home menu -> Setting -->Date & Time ->Un tick on 'Use 24- hour format'.).
Program
<TextClock

    android:id="@+id/hk_time"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:format12Hour="h:mm:ss a"

  />

Format 24 Hour
format clock according to 24 hour and specify format for that.
code - 
 
android:format24Hour="MMM dd, yyyy k:mm"

 
Note - In Some device format not support then use -
·         android:format12Hour="@null" .
or
·         Set Android phone in 24 hour format ( Home menu -> Setting -->Date & Time -> tick on 'Use 24- hour format'.).
Program
<TextClock

    android:id="@+id/hk_time"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:format24Hour="MMM dd, yyyy k:mm"

  />

TimeZpne
Clock show date and time according to timezone ( GMT or UTC).
<TextClock

    android:id="@+id/hk_time"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:timeZone="GMT+5:30"

    android:format24Hour="MMM dd, yyyy k:mm"

    android:format12Hour="@null" />






























1 comment: