Skip to content

Commit

Permalink
Added OnDateLongClickListener to the CalendarView features.
Browse files Browse the repository at this point in the history
Refactor of interface OnDateSelectedListener for OnDateClickListener.
  • Loading branch information
jonatan committed Jan 31, 2016
1 parent 9892c1b commit 14c5f6c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ calendarView.setNextButtonColor(R.color.colorAccent);
calendarView.refreshCalendar(Calendar.getInstance(Locale.getDefault()));
calendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull Date selectedDate) {
public void onDateClick(@NonNull Date selectedDate) {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
textView.setText(df.format(selectedDate));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ public void onClick(View view) {
calendarView.setBackButtonColor(R.color.colorAccent);
calendarView.setNextButtonColor(R.color.colorAccent);
calendarView.refreshCalendar(Calendar.getInstance(Locale.getDefault()));
calendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
calendarView.setOnDateLongClickListener(new CalendarView.OnDateLongClickListener() {
@Override
public void onDateSelected(@NonNull Date selectedDate) {
public void onDateLongClick(@NonNull Date selectedDate) {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
textView.setText(df.format(selectedDate));
}
});


calendarView.setOnMonthChangedListener(new CalendarView.OnMonthChangedListener() {
@Override
public void onMonthChanged(@NonNull Date monthDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void run() {
private ImageView mBackButton;

//Listeners used by the Calendar...
private OnDateSelectedListener mOnDateSelectedListener;
private OnDateClickListener mOnDateClickListener;
private OnDateLongClickListener mOnDateLongClickListener;
private OnMonthChangedListener mOnMonthChangedListener;

private Calendar mCalendar;
Expand Down Expand Up @@ -351,6 +352,7 @@ private void setDaysInCalendar() {

if (CalendarUtil.isSameMonth(calendar, startCalendar)) {
dayOfMonthContainer.setOnClickListener(onDayOfMonthClickListener);
dayOfMonthContainer.setOnLongClickListener(onDayOfMonthLongClickListener);
dayView.setBackgroundColor(mCalendarBackgroundColor);
mIsCommonDay = true;
if(totalDayOfWeekend().length != 0) {
Expand Down Expand Up @@ -464,6 +466,7 @@ private void setTotalDayOfWeekend() {
}
day++;
}

mTotalDayOfWeekend = weekendDay;
}

Expand Down Expand Up @@ -500,6 +503,33 @@ public void setDateAsSelected(Date currentDate) {
view.setTextColor(mSelectedDayTextColor);
}

private OnLongClickListener onDayOfMonthLongClickListener = new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// Extract day selected
ViewGroup dayOfMonthContainer = (ViewGroup) view;
String tagId = (String) dayOfMonthContainer.getTag();
tagId = tagId.substring(mContext.getString(R.string.day_of_month_container).length(), tagId.length());
final TextView dayOfMonthText = (TextView) view.findViewWithTag(mContext.getString(R.string.day_of_month_text) + tagId);

// Fire event
final Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(mFirstDayOfWeek);
calendar.setTime(mCalendar.getTime());
calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(dayOfMonthText.getText().toString()));
setDateAsSelected(calendar.getTime());

//Set the current day color
setCurrentDay(mCalendar.getTime());

if (mOnDateLongClickListener != null) {
mOnDateLongClickListener.onDateLongClick(calendar.getTime());
}

return false;
}
};

private OnClickListener onDayOfMonthClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -519,8 +549,9 @@ public void onClick(View view) {
//Set the current day color
setCurrentDay(mCalendar.getTime());

if (mOnDateSelectedListener != null)
mOnDateSelectedListener.onDateSelected(calendar.getTime());
if (mOnDateClickListener != null) {
mOnDateClickListener.onDateClick(calendar.getTime());
}
}
};

Expand Down Expand Up @@ -828,17 +859,35 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
*
* @author jonatan.salas
*/
public interface OnDateSelectedListener {
public interface OnDateClickListener {

/**
* Method that lets you handle
* when a user touches a particular date.
*
* @param selectedDate - the date selected by the user.
*/
void onDateSelected(@NonNull Date selectedDate);
void onDateClick(@NonNull Date selectedDate);
}

/**
* Interface that define a method to
* implement to handle a selected date event,
*
* @author jonatan.salas
*/
public interface OnDateLongClickListener {

/**
* Method that lets you handle
* when a user touches a particular date.
*
* @param selectedDate - the date selected by the user.
*/
void onDateLongClick(@NonNull Date selectedDate);
}


/**
* Interface that define a method to implement to handle
* a month changed event.
Expand All @@ -860,8 +909,12 @@ public interface OnMonthChangedListener {
/**
* Attributes setters and getters.
*/
public void setOnDateSelectedListener(OnDateSelectedListener onDateSelectedListener) {
this.mOnDateSelectedListener = onDateSelectedListener;
public void setOnDateClickListener(OnDateClickListener onDateClickListener) {
this.mOnDateClickListener = onDateClickListener;
}

public void setOnDateLongClickListener(OnDateLongClickListener onDateLongClickListener) {
this.mOnDateLongClickListener = onDateLongClickListener;
}

public void setOnMonthChangedListener(OnMonthChangedListener onMonthChangedListener) {
Expand Down

0 comments on commit 14c5f6c

Please sign in to comment.