diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPagerView.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPagerView.java index c88956b3..7685a6a0 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPagerView.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPagerView.java @@ -21,7 +21,7 @@ import static com.prolificinteractive.materialcalendarview.MaterialCalendarView.showOtherMonths; import static java.util.Calendar.DATE; -abstract class CalendarPagerView extends ViewGroup implements View.OnClickListener { +abstract class CalendarPagerView extends ViewGroup implements View.OnClickListener, View.OnLongClickListener { protected static final int DEFAULT_DAYS_IN_WEEK = 7; protected static final int DEFAULT_MAX_WEEKS = 6; @@ -77,6 +77,7 @@ protected void addDayView(Collection dayViews, Calendar calendar) { CalendarDay day = CalendarDay.from(calendar); DayView dayView = new DayView(getContext(), day); dayView.setOnClickListener(this); + dayView.setOnLongClickListener(this); dayViews.add(dayView); addView(dayView, new LayoutParams()); @@ -203,13 +204,23 @@ protected void invalidateDecorators() { } @Override - public void onClick(View v) { + public void onClick(final View v) { if (v instanceof DayView) { final DayView dayView = (DayView) v; mcv.onDateClicked(dayView); } } + @Override + public boolean onLongClick(final View v) { + if (v instanceof DayView) { + final DayView dayView = (DayView) v; + mcv.onDateLongClicked(dayView); + return true; + } + return false; + } + /* * Custom ViewGroup Code */ diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java index 225ac911..c364a9f1 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java @@ -221,6 +221,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse private CalendarDay maxDate = null; private OnDateSelectedListener listener; + private OnDateLongClickListener longClickListener; private OnMonthChangedListener monthListener; private OnRangeSelectedListener rangeListener; @@ -1372,6 +1373,15 @@ public void setOnDateChangedListener(OnDateSelectedListener listener) { this.listener = listener; } + /** + * Sets the listener to be notified upon long clicks on dates. + * + * @param longClickListener thing to be notified + */ + public void setOnDateLongClickListener(OnDateLongClickListener longClickListener) { + this.longClickListener = longClickListener; + } + /** * Sets the listener to be notified upon month changes. * @@ -1406,9 +1416,8 @@ public void setOnTitleClickListener(final OnClickListener listener) { * @param selected true if the day is now currently selected, false otherwise */ protected void dispatchOnDateSelected(final CalendarDay day, final boolean selected) { - OnDateSelectedListener l = listener; - if (l != null) { - l.onDateSelected(MaterialCalendarView.this, day, selected); + if (listener != null) { + listener.onDateSelected(MaterialCalendarView.this, day, selected); } } @@ -1446,9 +1455,8 @@ protected void dispatchOnRangeSelected(final CalendarDay firstDay, final Calenda * @param day first day of the new month */ protected void dispatchOnMonthChanged(final CalendarDay day) { - OnMonthChangedListener l = monthListener; - if (l != null) { - l.onMonthChanged(MaterialCalendarView.this, day); + if (monthListener != null) { + monthListener.onMonthChanged(MaterialCalendarView.this, day); } } @@ -1536,6 +1544,17 @@ protected void onDateClicked(final DayView dayView) { } + /** + * Call by {@link CalendarPagerView} to indicate that a day was long clicked and we should handle it + * + * @param dayView + */ + protected void onDateLongClicked(final DayView dayView) { + if (longClickListener != null) { + longClickListener.onDateLongClick(MaterialCalendarView.this, dayView.getDate()); + } + } + /** * Called by the adapter for cases when changes in state result in dates being unselected * diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/OnDateLongClickListener.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnDateLongClickListener.java new file mode 100644 index 00000000..4db93dd2 --- /dev/null +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnDateLongClickListener.java @@ -0,0 +1,18 @@ +package com.prolificinteractive.materialcalendarview; + +import android.support.annotation.NonNull; + +/** + * The callback used to indicate a date has been long clicked. + */ +public interface OnDateLongClickListener { + + /** + * Called when a user long clicks on a day. + * There is no logic to prevent multiple calls for the same date and state. + * + * @param widget the view associated with this listener + * @param date the date that was long clicked. + */ + void onDateLongClick(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date); +} diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivity.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivity.java index 77e47f44..70576709 100644 --- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivity.java +++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivity.java @@ -6,10 +6,8 @@ import android.support.v7.app.AppCompatActivity; import android.widget.TextView; -import com.prolificinteractive.materialcalendarview.CalendarDay; -import com.prolificinteractive.materialcalendarview.MaterialCalendarView; -import com.prolificinteractive.materialcalendarview.OnDateSelectedListener; -import com.prolificinteractive.materialcalendarview.OnMonthChangedListener; +import android.widget.Toast; +import com.prolificinteractive.materialcalendarview.*; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -20,7 +18,7 @@ /** * Shows off the most basic usage */ -public class BasicActivity extends AppCompatActivity implements OnDateSelectedListener, OnMonthChangedListener { +public class BasicActivity extends AppCompatActivity implements OnDateSelectedListener, OnMonthChangedListener, OnDateLongClickListener { private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance(); @@ -37,15 +35,22 @@ protected void onCreate(Bundle savedInstanceState) { ButterKnife.bind(this); widget.setOnDateChangedListener(this); + widget.setOnDateLongClickListener(this); widget.setOnMonthChangedListener(this); //Setup initial text - textView.setText(getSelectedDatesString()); + textView.setText("No Selection"); } @Override - public void onDateSelected(@NonNull MaterialCalendarView widget, @Nullable CalendarDay date, boolean selected) { - textView.setText(getSelectedDatesString()); + public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) { + textView.setText(selected ? FORMATTER.format(date.getDate()) : "No Selection"); + } + + @Override + public void onDateLongClick(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date) { + final String text = String.format("%s is available", FORMATTER.format(date.getDate())); + Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } @Override @@ -53,12 +58,4 @@ public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) { //noinspection ConstantConditions getSupportActionBar().setTitle(FORMATTER.format(date.getDate())); } - - private String getSelectedDatesString() { - CalendarDay date = widget.getSelectedDate(); - if (date == null) { - return "No Selection"; - } - return FORMATTER.format(date.getDate()); - } } diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java index 9058ede5..aaec19ca 100644 --- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java +++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java @@ -18,6 +18,8 @@ import com.prolificinteractive.materialcalendarview.CalendarMode; import com.prolificinteractive.materialcalendarview.MaterialCalendarView; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; @@ -25,8 +27,11 @@ import butterknife.ButterKnife; import butterknife.OnCheckedChanged; import butterknife.OnClick; +import com.prolificinteractive.materialcalendarview.OnDateLongClickListener; -public class DynamicSettersActivity extends AppCompatActivity { +public class DynamicSettersActivity extends AppCompatActivity implements OnDateLongClickListener { + + private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance(); @BindView(R.id.calendarView) MaterialCalendarView widget; @@ -48,10 +53,11 @@ protected void onCreate(Bundle savedInstanceState) { widget.setOnTitleClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { - Toast.makeText(DynamicSettersActivity.this, R.string.today, Toast.LENGTH_SHORT) - .show(); + Toast.makeText(DynamicSettersActivity.this, R.string.today, Toast.LENGTH_SHORT).show(); } }); + + widget.setOnDateLongClickListener(this); } @OnClick(R.id.button_other_dates) @@ -328,4 +334,9 @@ public static void showDatePickerDialog(Context context, CalendarDay day, ); dialog.show(); } + + @Override + public void onDateLongClick(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date) { + Toast.makeText(this, FORMATTER.format(date.getDate()), Toast.LENGTH_SHORT).show(); + } }