androidxamarin.androiddatepickerandroid-datepickerdatepickerdialog

Setformatter(new NumericMonthFormatter()) & SetDisplayValues(null) are not working in Android version 5.1.1


 NumberPicker numberPicker;
            if (datePickerNumericMonthFormat)
            {
                var input = numberPicker.FindViewById<EditText>(Activity.Resources.GetIdentifier("android:id/numberpicker_input", null, null));
                input.SetRawInputType(global::Android.Text.InputTypes.ClassNumber);

                if (monthSpinnerResId != 0)
                {
                    if (onCreateEventOrNot)
                    {
                        numberPicker = dialog.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId);
                    }
                    else
                    {
                        numberPicker = view.FindViewById<NumberPicker>(monthSpinnerResId);
                    }
                    numberPicker?.SetFormatter(new NumericMonthFormatter());
                    numberPicker?.SetDisplayedValues(null);
                    numberPicker.Invalidate();
                }
            }

public class NumericMonthFormatter: Java.Lang.Object, NumberPicker.IFormatter
        {
            public string Format(int value)
            {
                return string.Format("{0:D2}", (value + 1));
            }
        }

Solution

  • I have tested your code on different versions of Android. It can work well on the device which Android version is >= Android 7.1. And since the Android version 7.0 and lower. The error will appear.

    And I also converted your code to java and tested it in the native android project with the Android Studio. The result was the same.

    enter image description here

    It should be a bug in the native andorid. You can report it on the Google Android Community.

    In addition, the code caused this error is numberPicker?.SetDisplayedValues(null);. If you delete this line or use the following code, it can run on all the android versions.

    string[] arrays = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
    numberPicker?.SetDisplayedValues(arrays);