Home > OS >  DatePicker not showing correct year (2021 should be 2564) for Thai culture
DatePicker not showing correct year (2021 should be 2564) for Thai culture

Time:12-03

I am working on an App that supports various languages/cultures. But, the DatePicker controls seem buggy for the Thai culture:

enter image description here

I tried applying ThreadCulture using (also tried CultureInfo.CurrentCulture or CultureInfo.CurrentUICulture):

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture

and Applied ThaiBuddhhistCalendar:

_ = new System.Globalization.ThaiBuddhistCalendar();

but the widget Year always displays as 2021 instead of 2564.

Any hint to solve the issue would be really helpful.

CodePudding user response:

You could follow the stpes below.

  1. Change the year range of the picker. The default in from 1900 to 2100. You could set in Xamarin.Forms DatePicker in Xaml.

         <DatePicker MaximumDate="12/31/2600"></DatePicker>
    
  2. When you set the ThaiBuddhistCalendar, the DataPicker would would show the defalt DateTime with 2564 year in the Test. But when you open the dialog, you need to change the year in custom renderer.

     DatePickerDialog pickerDialog;
     private Xamarin.Forms.DatePicker datePicker;
    
     protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
     {
    
         pickerDialog = new DatePickerDialog(Context, (o, e) =>
         {
             datePicker.Date = e.Date.AddYears(-543);
             ((IElementController)datePicker).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
         }, year   543, month, day);
    
    
         return pickerDialog;
     }
    

    For more details about the custom renderer of DatePicker, you could check the code i done before. How to change DatePicker Ok and Cancel button text in Xamarin forms?

We only could change the year to 2564 in the pink part of you screenshot. In the center, the black one 2021, we could not change the default value at first time due to the native default value. But when we choose the 2564 again, the black one 2021 would be okay.

 private static final int DEFAULT_START_YEAR = 1900;
private static final int DEFAULT_END_YEAR = 2100; 

Xamarin.Android is only a layer of wrapper for Android native libraries, no extra operations, you could turn to Reporting Bugs | Android Open Source Project for help.

  • Related