Disable Past Date Picker (DPDP)

To disable past dates in Android date picker, you can use the following method. First, you need to get the current date using the Calendar class :

Calendar calendar = Calendar.getInstance();
 int yearNow = calendar. get (Calendar.YEAR);
 int monthNow = calendar. get (Calendar.MONTH);
 int dayNow = calendar. get (Calendar.DAY_OF_MONTH);

Then, when setting the date picker, use the setMaxDate() method by providing the current date as the upper limit:

DatePicker datePicker = findViewById(R.id.datePicker);

// Set the upper limit (maximum date) for the date picker.
datePicker.setMaxDate(calendar.getTimeInMillis());

This way, the date picker will only allow selecting dates that are the same or after the current date, leaving past dates unselected. Hope this helps!

Alternative

You can also use the following events

datePicker.setMinDate(System.currentTimeMillis() - 1000 );

Sets today's date as the minimum date and all past dates are disabled. datePicker is a DatePicker object if you are using a DatePickerDialog object you can do this.

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000 );

Post a Comment

Previous Next

نموذج الاتصال