Reputation: 674
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!
Upvotes: 2
Views: 3156
Reputation: 1333
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();
}
});
}
Upvotes: 6