0

I want to customize my TimePicker in .Net Maui to use the Spinner mode on Android(possible modes in android) and not the clock mode. Is this possible with Maui Handlers? I was looking for something like this

#if ANDROID
    Microsoft.Maui.Handlers.TimePickerHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
        {
            ??? handler.PlatformView.TimePickerMode ???;
        });
#endif

Or can I style it somehow?

Progman
  • 16,827
  • 6
  • 33
  • 48
Bongo
  • 2,933
  • 5
  • 36
  • 67

1 Answers1

0

The TimePicker's default mode is clock, and the attribute can just be set in the android layout xml or when it be created by calling the construction method. So you can't set it with the Muai Handler.

But you can use the android style to change the TimePicker's default mode.

  1. Create the style.xml under the /Platform/Android/Resource/values folder:

    <resources>
      <style name="Spinner" parent="android:Widget.Material.Light.TimePicker">
        <item name="android:timePickerMode">spinner</item>
      </style>
      <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:timePickerStyle">@style/Spinner</item>
      </style>
      <style name="Splash" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@android:color/holo_blue_dark</item>
        <item name="android:windowSplashScreenIconBackgroundColor">#000000</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/logo</item>
        <item name="windowSplashScreenAnimationDuration">300</item>
        <item name="postSplashScreenTheme">@style/AppTheme</item>
      </style>
    </resources>
    
  2. In the AndroidManifest.xml:

    <application android:allowBackup="true" android:theme="@style/Splash" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
    
  3. Add the package named AndroidX.Core.SplashScreen in your android part.

  4. Delete the (Theme="@style/Maui.SplashTheme") in the MainActivity and add the following code in the its OnCreate method's first line:

    AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen(this);
    

I have done a sample to test. The splash screen doesn't work when I set the item in the Maui.SplashTheme. So I declare another Theme for the application. But when I use the other control, the splash screen doesn't work.

So you can try to use the solution above or Just use the following code and set AppTheme as the MainActivity's theme. And then create a splash screen follwing the xamarin.android splash screen.

<resources>
  <style name="Spinner" parent="android:Widget.Material.Light.TimePicker">
     <item name="android:timePickerMode">spinner</item>
  </style>
  <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
     <item name="android:timePickerStyle">@style/Spinner</item>
  </style>
</resources>
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • 1
    *"So you can set it with the Muai Handler."* -- "can" or "can't"? – ToolmakerSteve Nov 01 '22 at 00:12
  • 1
    My careless, it's can't. @ToolmakerSteve – Liyun Zhang - MSFT Nov 01 '22 at 00:55
  • I don't want all my TimePicker to be Spinner. How would I use the creation method in Maui to create a Spinner-style Picker? – Bongo Nov 01 '22 at 12:25
  • If so, you can use the handler to custom an control and return a spinner by the TimePicker's construction method when you create the custom control. You can refer to [this link](https://github.com/dotnet/maui/wiki/Porting-Custom-Renderers-to-Handlers). @Bongo – Liyun Zhang - MSFT Nov 02 '22 at 07:55