SetFormatter method implementation for convert Number to string in Xamarin Android

Fair123 1 Reputation point
2022-11-16T15:07:59.093+00:00

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);  
        }  
    }  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,287 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 34,461 Reputation points Microsoft Vendor
    2022-11-18T06:05:47.053+00:00

    Hello,

    Actually, the code of the Formatter is working.

    If you use the following code, you could find the value of day picker has been changed as 01 02...

       private void setPickerStyle(DatePicker datePicker)  
       {  
           Resources mResources = Resources;  
           int id = mResources.GetIdentifier("day", "id", "android");  
           NumberPicker day = (NumberPicker)datePicker.FindViewById(id);  
           day.SetFormatter(new NumericMonthFormatter());  
       }  
    

    Here's why it doesn't work in the month picker.

    On native Android code, in the DatePicker constructor, the setDisplayedValues method is called to complete the setting of displaying month data.

    At the same time, the value listener of mMonthSpinner is onChangeListener, which will call back to the onValueChange method when the month changes, and the updateSpinners method will be called in this method, which is another place where the setDisplayedValues method is called directly.

    Calls in these places cause the values of the NumericMonthFormatter conversion to be overwritten.

    Therefore, you need to override those calls to set displayed values, you could refer to SetFormatter method implementation for convert Number to string in Xamarin Android.

    If you want to implement this functionality using Formatter, you need to implement your own DatePicker starting with layout.

    numberPicker.SetDisplayedValues(null);

    For the month selector, you must set an array of strings with 12 strings.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.