If you looking to block date on the Android date picker then don’t waste your time it’s not possible. There is no built-in feature by Android date picker to block dates. I am not talking about the past dates or the future dates. That can be easily done using setMinDate() or setMaxDate(). But to block specific dates or maybe a particular day in every week. In this case, you will have to use a custom date picker and wdullaer’s MaterialDateTimePicker provides a simple to do it.
Let’s say you want to block a specific date “25/06/2020”
First, you need to add the dependency in your module level gradle
1 |
compile 'com.wdullaer:materialdatetimepicker:3.3.0' |
Now instead of Android Date Picker, you will have to use wdullaer’s MaterialDateTimePicker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Calendar now = Calendar.getInstance(); com.wdullaer.materialdatetimepicker.date.DatePickerDialog dpd = com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance( your_on_date_set_listner, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) ); dpd.setVersion(com.wdullaer.materialdatetimepicker.date.DatePickerDialog.Version.VERSION_1); Calendar[] days; List<Calendar> blockedDays = new ArrayList<>(); // Code to disable particular date DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.US); Date date = formatter.parse("06/25/2020"); Calendar cal = Calendar.getInstance(); cal.setTime(date); blockedDays.add(cal); days = blockedDays.toArray(new Calendar[blockedDays.size()]); dpd.setDisabledDays(days); } |
That’s all !!! You can add as many Calender items in the list and block all the specific dates.
Thank you very much. This is Vedesh Kumar signing off.