I've implemented a DatePickerDialog
using the example shown here.
In my implementation of the DatePickerDialog.OnDateSetListener
I added validation logic to check that the selected date is within specific range.
private final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int y, int m,
int d) {
final Calendar calendar = Calendar.getInstance();
calendar.set(y, m, d);
Date date = calendar.getTime();
if(!myValidationFunction(date)) {
// date not within allowed range
// cancel closing of dialog ?
}
}
};
The problem I have is that the DatePickerDialog
is closed automaticlly when the user clicks the set button and I want to keep the DatePickerDialog
open if the validate rule fails.
Does anyone know how to stop the DatePickerDialog
from closing when the user clicks the Set button?
From API 11 the DatePicker can validate your date for you.
Following the guide you refer to, when overriding onCreateDialog, get DatePicker and set min and max date:
This way the user cannot pick a wrong date, thus no need to manually validate the date.
For older versions you can use a boolean for control when closing is allowed, and implement your own logic. Here I try to illustrate where you'll need to extend your code: