How to remove Datepickerdialog days?

Asked

Viewed 172 times

0

How can I remove the days of DatePickerDialog I wanted a calendar with only year and month, if anyone knows a way to do that and can help me?

1 answer

0

It is possible, I found a custom method once it removes the day, but I do not know how it behaves with the most current versions of DatePicker of the new versions of android.

private DatePickerDialog createDialogWithoutDateField() {

 Calendar calendar = Calendar.getInstance();
        DatePickerDialog dpd = new DatePickerDialog(this, null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    try {
        java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();

        for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {

            if (datePickerDialogField.getName().equals("mDatePicker")) {
                datePickerDialogField.setAccessible(true);
                DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
                java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();

                for (java.lang.reflect.Field datePickerField : datePickerFields) {
                    Log.i("test", datePickerField.getName());

                    if ("mDaySpinner".equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = datePickerField.get(datePicker);
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        }
    }
    catch (Exception ex) {
    }
    return dpd;
}

how this method returns an object DatePickerDialog, you can pass to your own date Picker or simply call the show():

createDialogWithoutDateField().show();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.