Reputation: 11
Background: I was working on an Android project where concurrencies in date booking will raise, and I have used MaterialDatePicker in order to choose the dates.
Problem: I need to DISABLE some SPECIFIC dates that are already selected by other users in order to handle the concurrencies. I am able to fetch the dates selected by other users from the firestore (Firebase - backend development platform), but unable to deal with the calender. I came across a blog which suggests to use setDisabledDays(calender[] days), but not so clearly how to do that?!
Need help with! - how do I disable specific dates on the calender???
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.clear();
long today = MaterialDatePicker.todayInUtcMilliseconds();
calendar.setTimeInMillis(today);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
long January = calendar.getTimeInMillis();
calendar.set(Calendar.MONTH, Calendar.DECEMBER);
long December = calendar.getTimeInMillis();
CalendarConstraints.Builder calenderConstraintBuilder = new CalendarConstraints.Builder();
calenderConstraintBuilder.setOpenAt(today);
calenderConstraintBuilder.setValidator(DateValidatorPointForward.now());
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("Select start date");
builder.setCalendarConstraints(calenderConstraintBuilder.build());
MaterialDatePicker materialDatePicker = builder.build();
mDatePickerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
});
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
@Override
public void onPositiveButtonClick(Object selection) {
duration = materialDatePicker.getHeaderText();
//This duration variable must be stored in database, it has both start and end date
dateResult.setText("Selected dates: " + duration);
}
});
Upvotes: 1
Views: 1047
Reputation: 1
My solution was to create a custom DateValidator when i wanted to block all the dates.
public class DateValidatorBlockAll implements CalendarConstraints.DateValidator {
public static final Parcelable.Creator<DateValidatorBlockAll> CREATOR = new Parcelable.Creator<DateValidatorBlockAll>() {
@Override
public DateValidatorBlockAll createFromParcel(Parcel parcel) {
return new DateValidatorBlockAll();
}
@Override
public DateValidatorBlockAll[] newArray(int size) {
return new DateValidatorBlockAll[size];
}
};
public DateValidatorBlockAll() {}
@Override
public boolean isValid(long date) {
/*
Add your logic here
if date == the dates you want to block
return false
else
return true
*/
return false;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
@Override
public int describeContents() {
return 0;
}}
And then replace this line of code
calenderConstraintBuilder.setValidator(DateValidatorPointForward.now());
with
calenderConstraintBuilder.setValidator(new DateValidatorBlockAll());
Upvotes: 0