Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable dates after today date in DatePickerDialog Android?

I want to disable all Next(after) dates from today date in DatePickerDialog.

I am using below code to create DatePickerDialog:

    Calendar calendar = Calendar.getInstance();
    mYear = calendar.get(Calendar.YEAR);
    mMonth = calendar.get(Calendar.MONTH);
    mDay = calendar.get(Calendar.DAY_OF_MONTH);

 //OnDateSetListener
 private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
        calendar.set(year, monthOfYear, dayOfMonth);
        String currentDate = date_format.format(calendar.getTime());
        ...
    }
};
 //Create DatePickerDialog here
 protected Dialog onCreateDialog(int id) {
    if (id == 999) {
        DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);

        return dpDialog;
    }
    return null;
}

Please suggest what I have to do for disable all previous date in datepicker dialog.

like image 654
pRaNaY Avatar asked Dec 02 '22 14:12

pRaNaY


1 Answers

Just pass current date in setMaxDate() of DatePickerDialog.

DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);
dpDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());

or

dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
like image 84
Ravi Avatar answered Dec 06 '22 12:12

Ravi