Select month and year range (without days) Android

11.1k views Asked by At

maybe someone know component (view) to select range? like:

Select from:      select to:
month+year        month+year

like this photo, but needed range selection

i finded datePicker views on gitHub only with days selection range: github

but in this component draws dates :(

2

There are 2 answers

2
Jan Imrich On BEST ANSWER

See this question: Android DatePicker change to only Month and Year

I would post that in comments, but I don't have permission to do so.

There is more options than in Pratik Kate's answer.

edit: I know those solutions do not offer range selection, but I think that could be easily solved using two pickers - 'from-to'.

See the screenshots e.g. in this project: Material Date and Time Picker with Range Selection

I think it can look pretty good. I can imagine many users would be confused when asked to select range using only one picker (having to use some sort of long-press or drag event?).

2
Pratik Kate On

Try the following code. It will show a DatePicker with only the year and month (without day)

private DatePickerDialog createDialogWithoutDateField() {
    DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
    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;
}

This method returns a date picker dialog. So , in your button's onClick method add the following code to display your dialog.

createDialogWithoutDateField().show();