Android: CalendarView OnDateChangeLIstener

17,579

Solution 1

When you are scrolling the calendar onSelectedDayChange method behaves like you click on different date but that won't change current date settings. So HFDO5 was right, you just need to save current date when you create your calendar: Long date = calendar.getDate();

and check it in onSelectedDayChange.

if(callendar.getDate() != date){
date = calendar.getDate(); //new current date
//date is changed on real click...do things..

}

Solution 2

I just had the same problem and found a work-around for it.

create "Long date" variable and when you start your calender window save the current date in this variable.

Long date;
cv = (CalendarView)findViewById(R.id.calendarView1);
date = cv.getDate();

now in the listener just check if the new date is same as the calendar:

cv.setOnDateChangeListener(new OnDateChangeListener(){
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
            if(cv.getDate() != date){
                date = cv.getDate();
                Toast.makeText(view.getContext(), "Year=" + year + " Month=" + month + " Day=" + dayOfMonth, Toast.LENGTH_LONG).show();
            }               
        }
    });

it worked for me :)

Solution 3

Finally I decided I needed to display an "OK" button below the CalendarView so the date can be chosen and the calendar dismissed.

Because when you click the current date nothing happens, and when you scroll you get calls to your OnDateChangeListener.

Example of calendar layout with black mask background you can click to dismiss:

<LinearLayout
    android:id="@+id/calendar_layout"
    android:onClick="dismissCalendar"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@color/black_mask">

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:showWeekNumber="false"
        android:firstDayOfWeek="2"
        />

    <TextView
        android:onClick="selectDate"
        android:text="@string/select_date"
        android:background="@color/colorPrimary"
        style="@style/settings_button"/>

</LinearLayout>

Then sample code:

private void openCalendar(Date date) {
    calendar.setDate(date.getTime());
    calendarLayout.setVisibility(View.VISIBLE);
}

public void selectDate(View view) {
    Date date = new Date(calendar.getDate());
    // do whatever you need with date
    dismissCalendar(view);
}

public void dismissCalendar(View view) {
    calendarLayout.setVisibility(View.INVISIBLE);
}
Share:
17,579
lemoncodes
Author by

lemoncodes

Updated on June 06, 2022

Comments

  • lemoncodes
    lemoncodes about 2 years

    I've already implemented this listener in order for me to display something when a certain date is clicked, but the problem is that when i scroll the CalendarView down, it automatically displayed something but i didn't click anything, i just scrolled down to anther month in CalendarView and then there goes a, say a Toast or a Log, whichever (I guess it makes sense since the listener itself fires `onDateChange and since scrolling down the calendar also changes the date currently selected). So my question is that is there any listener for CalendarView that i might use just a alternative to ondateChange listener, inorder to avoid the situation that when i scroll down the calendarView to go to another month it automatically fired the lisntener.

    Anyone who knows an alternative listner to CalendarView or anyone knows a workaround? please do share