Datepicker dialog without calendar visualization in lollipop [spinner mode]?

Evilripper picture Evilripper · Feb 26, 2015 · Viewed 74.5k times · Source

I read the documentation: http://developer.android.com/guide/topics/ui/controls/pickers.html but now in lollipop appears the calendar (it is ok for an event but horrible for set a date of birth, I would a spinner mode.) and I can't remove it! In layout It's easy with this property:

 <DatePicker
 datePickerMode="spinner"...>

but from code of the DatePickerDialog if I try to set

dialogDatePicker.getDatePicker().setSpinnersShown(true);
dialogDatePicker.getDatePicker().setCalendarViewShown(false); 

These properties do not work and the calendar continues to appear!

public static class MyDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        int pYear;
        int pDay;
        int pMonth;

        @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);
            DatePickerDialog dialogDatePicker = new DatePickerDialog(getActivity(), this, year, month, day);
            dialogDatePicker.getDatePicker().setSpinnersShown(true);
            dialogDatePicker.getDatePicker().setCalendarViewShown(false);
            return dialogDatePicker;
            // Create a new instance of DatePickerDialog and return it
            //return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            pYear = year;
            pDay = day;
            pMonth = month;
        }
    }

Answer

alanv picture alanv · Feb 27, 2015

DatePickerDialog uses the dialog theme specified by your activity theme. This is a fully-specified theme, which means you need to re-specify any attributes -- such as the date picker style -- that you've set in your activity theme.

<style name="MyAppTheme" parent="android:Theme.Material">
    <item name="android:dialogTheme">@style/MyDialogTheme</item>
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="MyDialogTheme" parent="android:Theme.Material.Dialog">
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="MyDatePicker" parent="android:Widget.Material.DatePicker">
    <item name="android:datePickerMode">spinner</item>
</style>

Note: Due to Issue 222208, this will not work in Android N / API 24. It has already been fixed in the platform for the next release. There is no workaround available for API 24 devices.