How to Disable Future Dates in Android Date Picker

How to Disable future dates in Android date picker

Get the DatePicker from DatePickerDialog with getDatePicker(). Set the max date to current date with setMaxDate():

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

Requires API level 11.

How to disable future dates in Android MaterialDatePicker?

To disable future dates just use the DateValidatorPointBackward provided by the library.

With today:

val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.now())

Sample Image

If you need a custom date (all dates after 1st April):

    val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar[Calendar.DAY_OF_MONTH] = 1
calendar[Calendar.MONTH] = Calendar.APRIL
val april = calendar.timeInMillis

val constraintsBuilder =
CalendarConstraints.Builder()
.setValidator(
DateValidatorPointBackward.before(april))

How to disable future date picker in android

You can simply use setMaxDate function for the date picker.

DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), date, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);  //date is dateSetListener as per your code in question
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

Refer documentation

Hope this helps.

Disable future dates in Android date picker

You should be able to call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your max date. You can update the function with the same name from the snippet you posted.

Note the DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.

@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
//start changes...
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
//end changes...
}
return null;
}

How to disable future date with Datepicker

After Declaring Listener on DatePicker use the mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

How to disable Future date in DatePicker in Kotlin Android

Try this

var dialog = DatePickerDialog(
this@MainActivity,dateSetListener,
// set DatePickerDialog to point to today's date when it loads up
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
)

dialog.datePicker.maxDate = Calendar.getInstance().timeInMillis
dialog.show()

Android Dynamically created Calendar disable future Dates

You can use setMaxDate() method of datepicker for that. I mentioned here, how to set max date before 18 year from now.

DatePickerDialog dialog = new DatePickerDialog(SecondActivity.this, date, myCalendar 
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));

Calendar calendar = Calendar.getInstance();
calendar.set(mYear - 18, mMonth, mDay);
dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
dialog.show();


Related Topics



Leave a reply



Submit