I want to disable the past dates of date picker in android. i can do it by using
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
This is working fine and the past dates in date picker looks disabled. But I can still click on a previous date and select it. How to not let that happen ? below is the screenshot of my date picker :-
And here is my code where I am disabling the past dates :-
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
mActivity = new WeakReference<CreateEvent>(
(CreateEvent) getActivity());
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
if(callingView==fromDate){
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}else if (callingView==toDate){
dialog.getDatePicker().setMinDate(fromD);
}
// Create a new instance of DatePickerDialog and return it
return dialog;
}
Got to know from a comment that this is a bug in Lollipop. So, fixed it programatically.
All you need to do is check the selected date with the min date set.
Yes it is a bug and you need to check the selected date using java code.
For past Date
public static boolean validatePastDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day > currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month > currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year > currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
For Future Date
public static boolean validateFutureDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day < currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month < currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year < currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Just pass the selected date month ,year in this method and will return boolean value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With