Skip to content

Commit

Permalink
Feature - Long Click Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin41500 committed Jun 27, 2018
1 parent 01c1c79 commit 95461fe
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +77,7 @@ protected void addDayView(Collection<DayView> 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());

Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -37,28 +35,27 @@ 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
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
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;

import butterknife.BindView;
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;
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 95461fe

Please sign in to comment.