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.
How to go to a selected date in Material Calendar View?
10.5k views Asked by Patriotic At
6
There are 6 answers
0
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.
0
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();
}
}
}
0
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
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.
When Button triggered, checked the counter and changed the view to current date using goToNext() and goToPrevious() method.
counter=0;