0

I cant seem to convert datepickerdialog into a spinner through xml. I am not currently using API 24 that has a bug. I try to do it by defining a new style and use it when instatiating DatePickerDialog like it is recommended here but it still does not work;

    <style name="CustomDatePickerDialog" parent="android:Theme.Material.Light.Dialog">
        <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
    </style>

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

here is the dialog definition;

dialog = new DatePickerDialog(CurrentActivity.this, R.style.CustomDatePickerDialog, new DatepickerDialog.OnDateSetListener() {...}, y,m,d);
dialog.show();

The weird thing is that if i define in code after the dialog instatiation and before showing it, these two lines, it will present me the spinner only, which is what i want;

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

i cant figure out why xml wont work on me, even though other people seem to be good with it. am i using wrong context? Any help is welcome.

EDIT: I'm having issue cause i don't want to use the deprecated two lines that solve my problem. So that means i don't want to use Theme_Holo_Light or other similar deprecated themes either.

1 Answers1

0

You can call this function and pass the datepicker instance as parameter.

public static void getDateYearMonth(Activity activity, String date, DatePickerDialog.OnDateSetListener mDateSetListener) {
            int year;
            int month;
            int day;
            if (date == null || date.isEmpty()) {
                Calendar cal = Calendar.getInstance();
                year = cal.get(Calendar.YEAR);
                month = cal.get(Calendar.MONTH);
                day = cal.get(Calendar.DAY_OF_MONTH);
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm");
                Date dateObj = null;
                try {
                    dateObj = sdf.parse(date);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dateObj);
                    year = cal.get(Calendar.YEAR);
                    month = cal.get(Calendar.MONTH);
                    day = cal.get(Calendar.DAY_OF_MONTH);
                } catch (ParseException e) {
                    e.printStackTrace();
                    Calendar cal = Calendar.getInstance();
                    year = cal.get(Calendar.YEAR);
                    month = cal.get(Calendar.MONTH);
                    day = cal.get(Calendar.DAY_OF_MONTH);
                }
            }

            DatePickerDialog dialog = new DatePickerDialog(
                    activity,
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                    mDateSetListener,
                    year, month, day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
        }

Hope this solves your problem. Happy Coding!!

Saugat Jonchhen
  • 356
  • 5
  • 16