dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.94k stars 533 forks source link

Android.Widget.TimePicker SetIs24HourView Does Not Work #8486

Closed Axemasta closed 1 year ago

Axemasta commented 1 year ago

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

net7.0-android

Description

Using the Android.Widget.TimePicker and trying to set it to scroll mode (12 hour) doesn't appear to work.

The use case is a custom view handler for a maui control. I tried the following axml in a file new android studio app:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TimePicker
            android:timePickerMode="spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

Which displays our nice scrolly time picker:

image

The TimePickerMode api appears to be missing in code, but it is documented as an xml property here.

In my Maui handler, I have the following layout:

public class MauiDateTimePicker : LinearLayout
{
    protected MauiDateTimePicker(IntPtr javaReference, JniHandleOwnership transfer) 
        : base(javaReference, transfer)
    {
    }

    public MauiDateTimePicker([NotNull] Context context) 
        : base(context)
    {
        var timePicker = new TimePicker(context);
        timePicker.SetIs24HourView(new Boolean(false));

        timePicker.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);

        AddView(timePicker);
    }

    public MauiDateTimePicker([NotNull] Context context, IAttributeSet attrs) 
        : base(context, attrs)
    {
    }

    public MauiDateTimePicker([NotNull] Context context, IAttributeSet attrs, int defStyleAttr) 
        : base(context, attrs, defStyleAttr)
    {
    }
}
image

As you can see, the time picker is still in 24 hour format.

Steps to Reproduce

This is reproduced in my proof of concept datetimepicker control repo (link). Run the sample and observe the code in the MauiDateTimePicker native view not being respected (link).

Did you find any workaround?

No

Relevant log output

N/A
jpobst commented 1 year ago

What should the time picker look like in 12 hour mode?

It looks like the time picker in your screenshot is 12 hour mode, because it offers "am" and "pm" options. I would assume if it was 24 hour mode those choices would not be available.

jpobst commented 1 year ago

If the actual issue is that you can't set timePickerMode to spinner from code, that seems to be something that Google is aware of and has chosen not to implement:

https://issuetracker.google.com/issues/37084372

Axemasta commented 1 year ago

What should the time picker look like in 12 hour mode?

In 12 hour it looked like: image

In 24 hour it looked like the normal time picker ui we see in xamarin android / maui etc: image

https://issuetracker.google.com/issues/37084372 This is for the TimePickerDialog not the TimePicker itself, although that seems like an absolute stinker of a decision from google to not fix.

😡 image

I tried subclassing TimePicker and overriding Is24HourView, still the undesired behaviour. I guess I'll have to use an axml file instead for this view which is... annoying 😬

Thanks for the help anyway!

jpobst commented 1 year ago

I think is24HourView and timePickerMode are unrelated. Both pictures show 12 hour views. What you want is timePickerMode = spinner instead of clock.

It does not appear timePickerMode can be set via code per Google.

jpobst commented 1 year ago

For example, a 24 hour "clock" TimePicker would look like this:

image

Axemasta commented 1 year ago

Ah I see, I must have got confused looking at the docs. I updated my lib to use an axml view and was able to change the mode to a spinner! :) (link).