2

I want to display a selected dates range when open the dateRangePicker, I knew the method setSelected is for this and I tried it its not work, or maybe am doing something wrong, please help

here what I tried already

public void openDateRangePicker(View view) {
 MaterialDatePicker.Builder builder =
                MaterialDatePicker.Builder.dateRangePicker();

        CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

        MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
        builder.setCalendarConstraints(constraintsBuilder.build());

        picker.setStyle(DialogFragment.STYLE_NORMAL, R.style.Custom_MaterialCalendar_Fullscreen);
        if(!firstDateStr.isEmpty() || !endDateStr.isEmpty()){
            builder.setSelection(selectionDates);

        }
        picker.show(getSupportFragmentManager(), picker.toString());



    picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
        @Override
        public void onPositiveButtonClick(Pair<Long, Long> selection) {

            long firstDateLong = selection.first;
            Date firstDate=new Date(firstDateLong);
            long endDateLong = selection.second;
            Date endDate=new Date(endDateLong);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

            //format yyyy-MM-dd
            firstDateStr = sdf2.format(firstDate);
            endDateStr = sdf2.format(endDate);
            selectionDates = selection;


            selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
            tvDates.setText(selectedDatesStr);
            tvDates.setTypeface(Typeface.DEFAULT_BOLD);
            picker.dismiss();

        }
    }); 

}

what I did here is save the selection object and use it to show the range when open the picker next time, but it opens without any selection like it open for the first time!

Rooh Al-mahaba
  • 594
  • 1
  • 14
  • 28
  • You can use this link...[datepicker custom date range](https://stackoverflow.com/questions/2526080/set-android-datepicker-date-limits) – Sanjay Ravichandran Jun 16 '20 at 22:44
  • @SanjayRavichandran sorry but I dont want to set min and max dates, and also the link is asking about date picker, what am asking here is date range picker, like here https://material.io/components/pickers – Rooh Al-mahaba Jun 16 '20 at 22:53

1 Answers1

6

order of your lines matters,you need to call builder.build(); after finishing setting up the builder.

  String firstDateStr="";
  String endDateStr="";

Pair<Long, Long> selectionDates=null;
public void openDateRangePicker() {

   MaterialDatePicker.Builder builder =
            MaterialDatePicker.Builder.dateRangePicker();

    CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

    builder.setCalendarConstraints(constraintsBuilder.build());

   // picker.setStyle(DialogFragment.STYLE_NORMAL);
    if(selectionDates!=null){
        builder.setSelection(selectionDates);

    }
    MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
    picker.show(getSupportFragmentManager(), picker.toString());



    picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
        @Override
        public void onPositiveButtonClick(Pair<Long, Long> selection) {

            long firstDateLong = selection.first;
            Date firstDate=new Date(firstDateLong);
            long endDateLong = selection.second;
            Date endDate=new Date(endDateLong);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

            //format yyyy-MM-dd
            firstDateStr = sdf2.format(firstDate);
            endDateStr = sdf2.format(endDate);
            selectionDates = selection;


            selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
           tvDates.setText(selectedDatesStr);
          tvDates.setTypeface(Typeface.DEFAULT_BOLD);
            picker.dismiss();

        }
    });

}
Code Demon
  • 1,256
  • 1
  • 9
  • 15