Tutorials Android How to Disable Past Dates in Custom Calendar View Android

How to Disable Past Dates in Custom Calendar View Android

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);
            }
        }
    }

}
Total
0
Shares
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JWest
JWest
6 years ago

How could I get the current selected date of the calendar in a Button event? I have a button below of the calendar. After the user click into calendar, He needs to click a button to go to next activity. I need to get the selected date clicked to pass as a bunlde. Is it possible? I have used Date dd=calendarView.getCurrentCalendar().getTime(); but it returns the current date not the selected date.

salem
salem
5 years ago

thank you for this article and your description

రామకృష్ణ శర్మ
రామకృష్ణ శర్మ
5 years ago

Hi i am not able to give current day background color can u please give code snippet, And each every day background color code i need to change can u help me

Previous Post

Validate Form Using JavaScript Regular Expressions Without jQuery

Next Post
Drupal Vs. Wordpress_ Which one to Choose

Drupal Vs. WordPress: Which one to Choose?

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
3
0
Would love your thoughts, please comment.x
()
x