2

I have replaced the default style of the data picker with the spinner one but now I need to change the date format (of the spinner not of the "result") to be the same in all cultures (dd/MM/yyyy).

I have tried to change the Format, but it's the format of the placeholder...

I'm sure that there is a way to change it in styles.xml but I don't know how to do it.

In styles.xml I have:

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

<style name="MyDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/MyDatePicker</item>
<item name="android:datePickerMode">spinner</item>
<item name="android:calendarViewShown">false</item>
<item name="android:spinnersShown">true</item>
</style>

<style name="MyDatePicker" parent="android:Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
<item name="android:calendarViewShown">false</item>
<item name="android:spinnersShown">true</item>
</style>

There is a way to change it here or in the custom renderer?

Elelio
  • 287
  • 5
  • 18

2 Answers2

1

I had the same problem, I found this answer useful but it's for native android, I had to do some changes for Xamarin.Forms Android, create a custom renderer and add these lines:

protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
    var dialog = base.CreateDatePickerDialog(year, month, day);
    dialog.Show();
    OrderDate(dialog, new char[] { 'd', 'm', 'y' });
    return dialog;
}

private void OrderDate(DatePickerDialog dialog, char[] ymdOrder)
{
    if (!dialog.IsShowing)
    {
        return;
    }


     int idYear = Resources.GetIdentifier("year", "id", "android");
     int idMonth = Resources.GetIdentifier("month", "id", "android");
     int idDay = Resources.GetIdentifier("day", "id", "android");
     int idLayout = Resources.GetIdentifier("pickers", "id", "android");

     NumberPicker spinnerYear = (NumberPicker)dialog.FindViewById(idYear);
     NumberPicker spinnerMonth = (NumberPicker)dialog.FindViewById(idMonth);
     NumberPicker spinnerDay = (NumberPicker)dialog.FindViewById(idDay);
     LinearLayout layout = (LinearLayout)dialog.FindViewById(idLayout);

    layout.RemoveAllViews();
    for (int i = 0; i < SPINNER_COUNT; i++)
    {
        switch (ymdOrder[i])
        {
            case 'y':
                layout.AddView(spinnerYear);
                setImeOptions(spinnerYear, i);
                break;
            case 'm':
                layout.AddView(spinnerMonth);
                setImeOptions(spinnerMonth, i);
                break;
            case 'd':
                layout.AddView(spinnerDay);
                setImeOptions(spinnerDay, i);
                break;
        }
    }
}

private void setImeOptions(NumberPicker spinner, int spinnerIndex)
{
    ImeAction imeOptions;
    if (spinnerIndex < SPINNER_COUNT - 1)
    {
        imeOptions = inputMethos.ImeAction.Next;
    }
    else
    {
        imeOptions = inputMethos.ImeAction.Done;
    }
    int idPickerInput = Resources.GetIdentifier("numberpicker_input", "id", "android");
    TextView input = (TextView)spinner.FindViewById(idPickerInput);
    input.SetImeActionLabel("",imeOptions);
}
Smeagol75
  • 26
  • 2
0

The DatePicker (Spinner) on Android defaults to the chosen settings on the User's phone. It is generally advised not to override native behavior.

You can however, override this behavior by creating a custom renderer to use a Custom DatePicker by following the steps shown here: https://stackoverflow.com/a/7208968/11104068

Saamer
  • 4,687
  • 1
  • 13
  • 55