My sample code below In OnCreateDialog event calling method below
SetNumericMonth(true, null, dialog);
private int monthSpinnerResId;
public void SetNumericMonth(bool onCreateEventOrNot, DatePicker view, DatePickerDialog dialog)
{
if (DatePickerNumericMonthFormat == false)
{
monthSpinnerResId = Activity.Resources.GetIdentifier("android:id/month", null, null);
if (monthSpinnerResId != 0)
{
var numberPicker = (NumberPicker)dialog.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId);
numberPicker.SetFormatter(new NumericMonthFormatter());
numberPicker.MinValue = 0;
numberPicker.MaxValue = 9;
}
}
}
public class NumericMonthFormatter: Java.Lang.Object, NumberPicker.IFormatter
{
public string Format1(int value)
{
return string.Format("{0:D2}", value);
}
}
You can try to create your DatePickerDialog and set the month number picker's display values. Such as:
public class MyDialog : DatePickerDialog
{
public MyDialog(Context context) : base(context)
{
}
public override void OnDateChanged(DatePicker view, int year, int month, int dayOfMonth)
{
base.OnDateChanged(view, year, month, dayOfMonth);
var monthSpinnerResId = Android.App.Application.Context.Resources.GetIdentifier("android:id/month", null, null);
var numberPicker = (NumberPicker)(this.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId));
string[] arrays = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
numberPicker.SetDisplayedValues(arrays);
}
}
And in the main activity:
MyDialog dialog = new MyDialog(this);
dialog.Show();
And there are some point for your code:
public class NumericMonthFormatter: Java.Lang.Object, NumberPicker.IFormatter
{
public string Format(int value)
{
return string.Format("{0:D2}", value);
}
}
In the styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:datePickerStyle">@style/MySpinnerDatePicker</item>
</style>
<style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
</resources>
If the default style is calendar, the var numberPicker = (NumberPicker)dialog.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId);
will get null.
In addition, if just use the formatter, the display values will be reset when the date changed. So I try to use the custom datepicker dialog.