3

I'm having trouble set max. and min. years in Material Design Datepickerdialog, also put an image of the Material Date picker dialog box where only one year is being shown.

Below is Code

    long today = MaterialDatePicker.todayInUtcMilliseconds();
            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());

              calendar.set(Calendar.MONTH, Calendar.JANUARY);
              long jan = calendar.getTimeInMillis();
                calendar.setTimeInMillis(today);


             int lastMonth = calendar.get(Calendar.MONTH);
                calendar.set(Calendar.MONTH, lastMonth);
                long dec = calendar.getTimeInMillis();


        CalendarConstraints.Builder calendarConstraintBuilder = new CalendarConstraints.Builder();
        calendarConstraintBuilder.setValidator(DateValidatorPointBackward.now());
        calendarConstraintBuilder.setStart(jan);
        calendarConstraintBuilder.setEnd(dec);

        MaterialDatePicker.Builder<Long> materialBuilder = MaterialDatePicker.Builder.datePicker();
        materialBuilder.setTitleText("Select Date Of Birth");
        materialBuilder.setSelection(today);


        materialBuilder.setCalendarConstraints(calendarConstraintBuilder.build());



        final MaterialDatePicker<Long> materialDatePicker = materialBuilder.build();
        inputEditTextDateOfBirth.setShowSoftInputOnFocus(false);

enter image description here

1 Answers1

3

For other readers, note that this problem pertains to the year selection after clicking on the month-year widget.

For year range selection purposes you have a line missing:

    // set minimum year variable
    int minYear = 2010;

    // here you set the month using the "current" calendar instance
    calendar.set(Calendar.MONTH, Calendar.JANUARY);

    // but you need to also set the year of "min"
    calendar.set(Calendar.YEAR, minYear);

    // and then get a snapshot of that time.
    long jan = calendar.getTimeInMillis();

I'm not sure if that completely satisfies your requirements but at a minimum it presents a range of years. The display of text was part of my test driver using addOnPositiveButtonClickListener:

enter image description here

And as I was curious, this also prevents the "month selector arrows" from advancing earlier than "min":

enter image description here

Computable
  • 976
  • 2
  • 4
  • 16