masteroleary masteroleary - 3 months ago 336
Android Question

How to set the android datepicker to spinner (not calendar) mode in Xamarin Forms?

When selecting the datepicker control in Xamarin Forms on an android device the Calendar Mode view of the datepicker is shown. How can I change it to show the Spinner Mode?

The only sample I found was to update my styles.xml

Here is what I have, which is not working

<resources>
<style name="MyTheme.Base"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>

<style name="MyTheme" parent="MyTheme.Base">
<item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
<item name="android:dialogTheme">@style/MyDialogTheme</item>
<item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="MyTheme.ActionBarStyle"
parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:background">@color/material_blue_500</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@android:color/white</item>
</style>

<style name="MyDialogTheme" parent="android:Theme.Material.Dialog">
<item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="MyDatePicker" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
</resources>

Answer

Here is the Resources/values/styles.xml that worked for me

<?xml version="1.0" encoding="utf-8" ?>
<resources>

    <style name="MyTheme.Base" parent="@android:style/Theme.Holo.Light.DarkActionBar">

        <item name="android:dialogTheme">@style/MyDialogTheme</item> 
    </style>

    <style name="MyTheme" parent="MyTheme.Base">
        <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
        <item name="android:dialogTheme">@style/MyDialogTheme</item> 
    </style>

    <style name="MyDialogTheme" parent="android:Theme.Material.Dialog">
        <item name="android:datePickerStyle">@style/MyDatePicker</item>
    </style>

    <style name="MyDatePicker" parent="android:Widget.Material.DatePicker">
        <item name="android:datePickerMode">spinner</item>
    </style>
    <style name="cust_tabViewStyle">
        <item name="android:textColor">@color/material_blue_500</item> 
    </style>

</resources>

Then set the theme in the Properties/AndroidManifest.xml by modifying the application node to this

<application android:label="MyApp" android:icon="@drawable/Icon" android:theme="@style/MyTheme">
Comments