6

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?

Mulgard
  • 9,877
  • 34
  • 129
  • 232
  • Try setting `android:calendarViewShown="false" android:datePickerMode="spinner"` in xml – Apurva Feb 26 '15 at 11:13
  • i initialize my datepicker programmatically and it should stay like that. there is no option to set datePickerMode when initializing programmatically – Mulgard Feb 26 '15 at 12:18
  • You have set `datePickerDialog.getDatePicker().setCalendarViewShown(false);` which will not work for API<11 – Apurva Feb 26 '15 at 14:48
  • I just support Api 17 and above. only version 21 is showing the calendar. other versions do not. – Mulgard Feb 26 '15 at 16:28
  • Possible duplicate of [Android Material Design Inline Datepicker issue](http://stackoverflow.com/questions/26854762/android-material-design-inline-datepicker-issue) – HaydenKai Jun 02 '16 at 10:51

2 Answers2

10

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>
Evren Ozturk
  • 918
  • 2
  • 19
  • 39
-1

This has been answered elsewhere: Android Material Design Inline Datepicker issue, and trying to solve it with the accepted answer got me no where.

Basically you need to do:

<DatePicker
 ...
 android:datePickerMode="spinner" />

because setCalandarShown(); has been deprecated.

Community
  • 1
  • 1
HaydenKai
  • 871
  • 7
  • 31
  • https://stackoverflow.com/questions/34723495/how-to-set-datepickermode-spinner-programmatically – HaydenKai Jul 27 '17 at 05:56
  • that might help @TashPemhiwa but these things don't seem reliable at present, especially when they need to be backwards compatible – HaydenKai Jul 27 '17 at 05:56