I searched a lot on the internet and I couldn\'t succeed to find correct solution for CalendarView click on today date.
I made the used the following class. It is a bit of a hack with the thread sleep, but one can increase the sleep time.
public class CustomCalendarView extends CalendarView {
private final String LOG_HEADER = "CAL:VW";
private Date previousSelectedDate = new Date();
private OnDateChangeListener listener;
private CheckSameSelectedDateAsyncTask task = new CheckSameSelectedDateAsyncTask();
public CustomCalendarView(Context context) {
super(context);
}
public CustomCalendarView(Context context, AttributeSet attribute) {
super(context, attribute);
}
public CustomCalendarView(Context context, AttributeSet attribute, int defStyle) {
super(context, attribute, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if ((task.getStatus() == AsyncTask.Status.PENDING) || (task.getStatus() == AsyncTask.Status.FINISHED)) {
task = new CheckSameSelectedDateAsyncTask();
task.execute();
}
return false;
}
private void checkSameSelectedDate() {
Date selectedDate = new Date(super.getDate());
if (selectedDate.getDay() == previousSelectedDate.getDay() &&
selectedDate.getMonth() == previousSelectedDate.getMonth() &&
selectedDate.getYear() == previousSelectedDate.getYear()) {
if (listener != null) {
this.listener.onSameSelectedDayChange(this, selectedDate.getYear(), selectedDate.getMonth(), selectedDate.getDay());
}
}
this.previousSelectedDate = selectedDate;
}
public void setSameSelectedDayChangeListener(OnDateChangeListener listener) {
this.listener = listener;
}
public interface OnDateChangeListener extends CalendarView.OnDateChangeListener {
void onSameSelectedDayChange(CalendarView view, int year, int month, int day);
}
private class CheckSameSelectedDateAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
//Note: Breaking point between 75 - 100
Thread.sleep(300);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
checkSameSelectedDate();
}
}
}
Try this with below way:
//First set current day,month,year :
Calendar c = Calendar.getInstance();
int prevDay = c.get(Calendar.DAY_OF_MONTH);
int prevMonth = c.get(Calendar.MONTH);
int prevYear = c.get(Calendar.YEAR);
Date d = new Date(prevYear, prevMonth, prevDay);
c.setTime(d);
Then use dateChangeListener :
CalendarView calendar = (CalendarView) findViewById(R.id.calendarViewCN);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,int month, int dayOfMonth) {
<b>boolean changed = changeUpdate(year, month, dayOfMonth);</b>
if (changed) {
Toast.makeText(getApplicationContext(),
"New Y: " + year + " M: " + month + " D: "+dayOfMonth, Toast.LENGTH_LONG).show();
}
}
});
Now declare the changeUpdate() :
private boolean changeUpdate(int curYear, int curMonth, int curDay) {
boolean changed = false;
if (curDay != prevDay || curMonth!=prevMonth || curYear!=prevYear) {
changed = true;
}