Skip to content Skip to sidebar Skip to footer

How To Go To A Selected Date In Material Calendar View?

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

Solution 1:

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.

Solution 2:

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

privatevoidselectDate(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();
        } elseif (diffMonth > 0) {
            calendarView.goToPrevious();
        }
    }
}

Solution 3:

First find material calendar view after using this:

calendarView = (MaterialCalendarView)findViewById(R.id.calendarView);
calendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);

Solution 4:

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
        publicvoidonMonthChanged(MaterialCalendarView widget, CalendarDay date) {
            if(date.getYear()==currentCal.get(Calendar.YEAR)){
                if(date.getMonth()<currentCal.get(Calendar.MONTH)){
                    --counter;
                }elseif(date.getMonth()>currentCal.get(Calendar.MONTH)){
                    ++counter;
                }
            }elseif(date.getYear()<currentCal.get(Calendar.YEAR)){
                --counter;
            }elseif(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();
        }elseif(counter>0){
            calendarView.goToPrevious();
        }
    }
    

    counter=0;

Solution 5:

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

Post a Comment for "How To Go To A Selected Date In Material Calendar View?"