Last year, I have published the CustomCalendarView Open Source Android library. This library makes it easy to add a Calendar to your Android application.
Since then many people are asking questions like how to use Decorators and how to disable some dates in CustomCalendarView. This post answers that.
In this example, we will disable all the dates that are past.
public class CalendarDayDecoratorActivity extends AppCompatActivity { private CustomCalendarView calendarView; private TextView selectedDateTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calendar_decorator); selectedDateTv = (TextView) findViewById(R.id.selected_date); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); //Initialize CustomCalendarView from layout calendarView = (CustomCalendarView) findViewById(R.id.calendar_view); //Initialize calendar with date Calendar currentCalendar = Calendar.getInstance(Locale.getDefault()); //Show monday as first date of week calendarView.setFirstDayOfWeek(Calendar.MONDAY); //Show/hide overflow days of a month calendarView.setShowOverflowDate(false); //call refreshCalendar to update calendar the view calendarView.refreshCalendar(currentCalendar); //Handling custom calendar events calendarView.setCalendarListener(new CalendarListener() { @Override public void onDateSelected(Date date) { if (!CalendarUtils.isPastDay(date)) { SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); selectedDateTv.setText("Selected date is " + df.format(date)); } else { selectedDateTv.setText("Selected date is disabled!"); } } @Override public void onMonthChanged(Date date) { SimpleDateFormat df = new SimpleDateFormat("MM-yyyy"); Toast.makeText(CalendarDayDecoratorActivity.this, df.format(date), Toast.LENGTH_SHORT).show(); } }); //adding calendar day decorators List<DayDecorator> decorators = new ArrayList<>(); decorators.add(new DisabledColorDecorator()); calendarView.setDecorators(decorators); calendarView.refreshCalendar(currentCalendar); } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle arrow click here if (item.getItemId() == android.R.id.home) { finish(); // close this activity and return to preview activity (if there is any) } return super.onOptionsItemSelected(item); } private class DisabledColorDecorator implements DayDecorator { @Override public void decorate(DayView dayView) { if (CalendarUtils.isPastDay(dayView.getDate())) { int color = Color.parseColor("#a9afb9"); dayView.setBackgroundColor(color); } } } }