Impossible to make my DatePickerDialog use a spinner style programmatically

Lumberjack picture Lumberjack · May 28, 2015 · Viewed 14.2k times · Source

I'm using a DialogFragment to open a DatePickerDialog

public class DatePickerFragment extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog DatePickerDialog = new DatePickerDialog(getActivity(), (ProfileCreationActivity)getActivity(), year, month, day);
    return DatePickerDialog;
}

I'm getting a calendar look, where I would prefer a spinner look.

I tried:

datePickerDialog.getDatePicker().setCalendarViewShown(false);

and

datePickerDialog.getDatePicker().setLayoutMode(1);

but it does not work.

Please note that I want the spinner look for one activity, but that I will want the calendar view for another activity. So I can not change the whole application style. I need a custom style for one activity.

Answer

Lumberjack picture Lumberjack · Jul 12, 2015

I have found a the explanation in the following post (which describes a problem very similar to mine) :

Android Material Design Inline Datepicker issue

In fact the setCalendarViewShown(false) and setSpinnersShown(true) are apparently not working anymore in latest versions.

We have to use an explicit XML attribute like this one android:datePickerMode="spinner".

The problem is that I'm using a DialogFragment without any XML layout (just a date picker dialog). So I cannot set any XML attribute.

The solution is to create a dedicated custom dialog with an XML layout file using the requested attribute.