I want to restrict datepicker entries based on FROM and TO date. The code below works just fine but in UI the disabled dates can be selected when it should be disabled and non clickable.
Here is the code :
if (v == fromDateButton || v == toDateButton) {
final Calendar now = GregorianCalendar.getInstance();
mYear = now.get(Calendar.YEAR);
mMonth = now.get(Calendar.MONTH);
mDay = now.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
String day = "", month = "";
monthOfYear = (monthOfYear + 1); // Calendar returns 1st month as 0
if (monthOfYear <= 9)
month = "0" + monthOfYear;
else month = monthOfYear + "";
if (dayOfMonth <= 9)
day = "0" + (dayOfMonth);
else day = (dayOfMonth) + "";
if (v == fromDateButton)
fromDateField.setText(day + "/"
+ month + "/" + year);
else toDateField.setText(day + "/"
+ month + "/" + year);
// Enable todate field
toDateButton.setEnabled(true);
}
}, mYear, mMonth, mDay);
if (!toDateButton.isEnabled()) {
dpd.getDatePicker().setMaxDate(new Date().getTime());
}
if (v == toDateButton) {
// if (fromDateField.getText() != null && fromDateField.getText() != "") {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date;
try {
date = df.parse(fromDateField.getText().toString());
dpd.getDatePicker().setMinDate(date.getTime());
dpd.getDatePicker().setMaxDate(new Date().getTime());
} catch (ParseException e) {
e.printStackTrace();
}
// }
}
if (v == fromDateButton) {
// if (toDateField.getText() != null && toDateField.getText() != "") {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date;
try {
date = df.parse(toDateField.getText().toString());
dpd.getDatePicker().setMaxDate(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
// }
}
dpd.show();
}
And here is the picker from which a disabled field can be selected
Any idea why it's happening ? I just need to restrict user from selecting disabled dates.