Disable future dates from DatePickerDialog in Android

As we already know DatePickerDialog will be used to allow user to select date. It is useful when user wants to select Date of Birth or Start Date or End Date etc.

Here is the small code snippet of creating DatePickerDialog.

and to listen date selection event here is the listener

With the above code you can select date from past as well as from future also, but when it comes to the DOB(Date of Birth) you can’t allow user to select future date.

In solution to this problem DatePickerDialog has a method getDatePicker() through which we can get DatePicker obeject and with that we can use setMaxDate() which will not allow user to select the date beyond the given date.

Here we have taken current time, so user will not be allowed to select future dates.

getResources().getColor() is deprecated
INVISIBLE vs GONE View in Android