Reputation: 2421
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
Upvotes: 16
Views: 19172
Reputation: 11529
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);
}
Upvotes: 3
Reputation: 225
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..
}
Upvotes: 8
Reputation: 902
Maybe my solution to my previous problem will give you some idea. When I pick a date from the calendar view, a google map is displayed showing the route that I walked for that date. Because the latitude and longitude are retrieved from database, so I query the database for that date and if there's no record the app pops up a toast message. Otherwise it shows a google map with the route.
Upvotes: -1
Reputation: 1582
My way around this was using calendar from HoloEverywhere (available source code for customization), commenting out the code causing this to be invoked and invoking it in onDateTapped method, that does the expected thing. And it works on pre ICS devices too.
Upvotes: 0
Reputation: 129
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 :)
Upvotes: 7