Raj Rajput
Raj Rajput

Reputation: 57

How to change format of TextClock in android with Kotlin

<TextClock
        android:layout_marginTop="20dp"
        android:id="@+id/textClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="45dp"
        android:format12Hour="hh:mm:ss a"
        android:layout_gravity="center"/>

This is my TextClock in my layout file I wanted to programmatically change its format from 12 hours to 24 hours

So far I have tried this:

var textClock: TextClock = view.findViewById<TextClock>(R.id.textClock)
textClock.format24Hour = "hh:mm:ss a"

But nothing seems to be working. There is a method called setFormat24Hour(CharSequence format) in java but in Kotlin I am not able to achieve the desired result.

Your help will be appreciated. Thanks

Upvotes: 0

Views: 1322

Answers (1)

Henry Twist
Henry Twist

Reputation: 5980

As the documentation says, the setFormat24Hour(CharSequence) method:

Specifies the formatting pattern used to display the date and/or time in 24-hour mode.

It doesn't actually change the clock to a 24 hour one. Instead the TextClock uses the system default setting to decide which format to use.

However if you really want to enforce the format, you should set the format of the other mode to null, which will force it to display how you want.

Upvotes: 2

Related Questions