Mulgard Mulgard - 2 months ago 53
Android Question

DatePicker in a DialogFragment ignores setCalendarViewShown

I wanted to show a DatePicker in a DialogFragment:

public class DatePickerDialogFragment extends DialogFragment {
private OnDateSetListener dateSetListener = null;
private String title = null;

public DatePickerDialogFragment() {}

public DatePickerDialogFragment(OnDateSetListener dateSetListener, String title) {
this.dateSetListener = dateSetListener;
this.title = title;
}

public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this.getActivity(), this.dateSetListener, year, month, day);
datePickerDialog.getDatePicker().setCalendarViewShown(false);
datePickerDialog.setTitle(this.title);

return datePickerDialog;
}
}


Unfortunately
datePickerDialog.getDatePicker().setCalendarViewShown(false);
is completly ignored. I hate the calendar view because it is ugly and uncomfortable. So how can i disable it?

Answer

In my "values-v21/styles.xml" I've modified activity theme as below it's hiding calendar & shows simple datepicker spinner. You might want to use a drawable with white background & rounded corners:

    <item name="android:datePickerDialogTheme">@style/style_date_picker_dialog</item>
</style>


<style name="style_date_picker_dialog" parent="@android:style/Theme.DeviceDefault.Light">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowBackground">@drawable/dialog_background</item>
    <item name="android:datePickerStyle">@style/style_datepicker</item>
</style>

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