To disable every Sunday in calendar view in android

How to disable only sunday in the following code? We cannot find any solution to turn off Sunday during the month

MainActivity.java:

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar = Calendar.getInstance();
calendarView.setMinDate(calendar.getTimeInMillis());
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() 
{

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Toast.makeText(getApplicationContext(), "" + dayOfMonth, 0).show();// TODO Auto-generated method stub`enter code here`

    }
});
+4
source share
2 answers

I used this code in the project. See if it creates the desired result -

//Global Variables
private Calendar lastSelectedCalendar = null;
private CalendarView calendarView;
//

calendarView = (CalendarView) findViewById(R.id.calendarView);
lastSelectedCalendar = Calendar.getInstance();
calendarView.setMinDate(lastSelectedCalendar.getTimeInMillis() - 1000);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Calendar checkCalendar = Calendar.getInstance();
        checkCalendar.set(year, month, dayOfMonth);
        if(checkCalendar.equals(lastSelectedCalendar))
            return;
        if(checkCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            calendarView.setDate(lastSelectedCalendar.getTimeInMillis());
        else
            lastSelectedCalendar = checkCalendar;
    }
});
+1
source
Calendar sunday;
List<Calendar> weekends = new ArrayList<>();
int weeks = 5;

for (int i = 0; i < (weeks * 7) ; i = i + 7) {
    sunday = Calendar.getInstance();
    sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
    // saturday = Calendar.getInstance();
    // saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
    // weekends.add(saturday);
    weekends.add(sunday);
}
Calendar[] disabledDays = weekends.toArray(new Calendar[weekends.size()]);
dpd.setDisabledDays(disabledDays);

This code disables the next 5 Sundays, if you want to do this for a longer period of time, you just need to change the week. If you want to disable Saturday just uncomment these lines.

If you want to do this in the previous 5 Sundays, just change the for loop to:

 for (int i = 0; i < (weeks * 7); i = i + 7) {
for(int j =0; j > (weeks*7) ; j = j - 7);
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
    // saturday = Calendar.getInstance();
    // saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
    // weekends.add(saturday);
    weekends.add(sunday);
}
0
source

Source: https://habr.com/ru/post/1695714/


All Articles