Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to disable some day from date picker I have an array that store the days I want to disable.
so i mean that i want disable for example 3 day (Sunday,Friday,Wednesday) how can i do this please help me....

I have this code to disable the days from calendar

for (Calendar loopdate = min_date_c;
                    min_date_c.before(max_date_c);
                    min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
                   int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);


                   if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
                       Calendar[] disabledDays = new Calendar[1];
                       disabledDays[0] = loopdate;
                       datePickerDialog.setDisabledDays(disabledDays);
                   }
               }


What I have tried:

and I tried this code to disable days that are stored in the arraylist, but it does not work. It only disables the first item from the arraylist

ArrayList a=new ArrayList();
            a.add(1);  //Saturday
            a.add(2);  //Sunday
            a.add(3);  //Monday
            int day;


            for(int i=0; i<a.size();i++) {

                day=(Integer)a.get(i);



                for (Calendar loopdate = min_date_c;
                     min_date_c.before(max_date_c);
                     min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
                    int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);



                    if (day == 1) {
                        System.out.println(day);
                        if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
                            Calendar[] disabledDays = new Calendar[1];
                            disabledDays[0] = loopdate;
                            datePickerDialog.setDisabledDays(disabledDays);
                        }
                    }

                  else  if (day==2) {
                        System.out.println(day);

                        if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.FRIDAY) {
                            Calendar[] disabledDays = new Calendar[1];
                            disabledDays[0] = loopdate;
                            datePickerDialog.setDisabledDays(disabledDays);
                        }
                    }

                   else if (day==3) {
                        System.out.println(day);
                        if (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.FRIDAY) {
                            Calendar[] disabledDays = new Calendar[1];
                            disabledDays[0] = loopdate;
                            datePickerDialog.setDisabledDays(disabledDays);
                        }
                    }


                }
Posted
Comments
David Crow 5-Mar-20 15:36pm    
Have you tried extending a class from DatePickerDialog? I've not looked, but that might give you the ability to do some initialization before the UI is actually shown.

If not, you might have to reconsider your approach, or put together a custom "date picker" control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900