How to go to a selected date in Material Calendar View?

10.5k views Asked by At

I'm using material-calendarview in my project. I can change the selection of the date using setSelectedDate() method. I've a button as "Today selection".My intention is to move the views to current date by clicking this button.There are methods goToNext() and goToPrevious() which only changes one month.But if it is two or more month forward or backward,then how should i change the view to selected date or current date.I tried using invalidate() but nothing happens.

6

There are 6 answers

2
Patriotic On

I've handled this issue somehow so, i thought i should share it with community.

  • Global counter variable to keep track of month.

    private int counter;

  • setOnMonthChangedListener() to listen the month

  • Compare date with current date.

    final Calendar currentCal=Calendar.getInstance();
    currentCal.set(Calendar.DATE,1);
    
    calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
        @Override
        public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
            if(date.getYear()==currentCal.get(Calendar.YEAR)){
                if(date.getMonth()<currentCal.get(Calendar.MONTH)){
                    --counter;
                }else if(date.getMonth()>currentCal.get(Calendar.MONTH)){
                    ++counter;
                }
            }else if(date.getYear()<currentCal.get(Calendar.YEAR)){
                --counter;
            }else if(date.getYear()>currentCal.get(Calendar.YEAR)){
                ++counter;
            }
        }
    });
    
  • When Button triggered, checked the counter and changed the view to current date using goToNext() and goToPrevious() method.

    int c=Math.abs(counter);
    for (int i = 0; i <c ; i++) {
        if(counter<0){
            calendarView.goToNext();
        }else if(counter>0){
            calendarView.goToPrevious();
        }
    }
    

    counter=0;

0
ralphgabb On

For those people who are having a hard time on this one, update your gradle to :

compile 'com.prolificinteractive:material-calendarview:1.4.2'

there is a function :

calendarView.setCurrentDate(CalendarDay.from(year, month, day), true);

no more computation, pager directs to selected date. kudos.

2
ananth kumar On

First find material calendar view after using this:

calendarView = (MaterialCalendarView)findViewById(R.id.calendarView);
calendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
0
Lance Johnson On

I did something similar to @Patriotic but was able to do it off current state of calendar rather than a listener and counter.

private void selectDate(Date date) {
    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(date);
    Calendar endCalendar = calendarView.getCurrentDate().getCalendar();

    calendarView.setSelectedDate(date);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
    int monthsToMove = Math.abs(diffMonth);
    for (int i = 0; i < monthsToMove; i++) {
        if (diffMonth < 0) {
            calendarView.goToNext();
        } else if (diffMonth > 0) {
            calendarView.goToPrevious();
        }
    }
}
2
Descend On
//this is default
calendarMonth.currentDate = CalendarDay.today()

//ok?
calendarMonth.selectedDate = CalendarDay.today()
0
Mohamed Emish On

All answers not do what is needed the solution is:

 val current = calendar.time
    calendarMaterial!!.selectionMode = SELECTION_MODE_RANGE
    calendarMaterial!!.setCurrentDate(CalendarDay.from(current),true)
    calendarMaterial!!.state().edit()
                .setMinimumDate(current)
                .commit()

this will prevent old date from selection but currently it sits it to unvisible .. i am working on just set it to gray