diff --git a/app/src/main/java/io/blackbox_vision/materialcalendarview/sample/view/MainActivity.java b/app/src/main/java/io/blackbox_vision/materialcalendarview/sample/view/MainActivity.java index 7cd54b2..27fb330 100644 --- a/app/src/main/java/io/blackbox_vision/materialcalendarview/sample/view/MainActivity.java +++ b/app/src/main/java/io/blackbox_vision/materialcalendarview/sample/view/MainActivity.java @@ -32,6 +32,7 @@ public final class MainActivity extends AppCompatActivity implements MainView { private static final String DATE_TEMPLATE = "dd/MM/yyyy"; + private static final String MONTH_TEMPLATE = "MMMM yyyy"; private final MainPresenter presenter = new MainPresenter(this); @@ -53,9 +54,6 @@ public final class MainActivity extends AppCompatActivity implements MainView { @BindView(R.id.calendar_view) CalendarView calendarView; - @NonNull - private final SimpleDateFormat formatter = new SimpleDateFormat(DATE_TEMPLATE, Locale.getDefault()); - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -86,27 +84,24 @@ public boolean onNavigationItemSelected(MenuItem item) { @Override public void prepareTextView() { - textView.setText(String.format("Today is %s", formatter.format(new Date(System.currentTimeMillis())))); + textView.setText(String.format("Today is %s", formatDate(DATE_TEMPLATE, new Date(System.currentTimeMillis())))); } @Override public void prepareCalendarView() { - final ActionBar actionBar = getSupportActionBar(); + calendarView.setFirstDayOfWeek(Calendar.MONDAY) + .setOnDateClickListener(this::onDateClick) + .setOnMonthChangeListener(this::onMonthChange) + .setOnDateLongClickListener(this::onDateLongClick) + .setOnMonthTitleClickListener(this::onMonthTitleClick); + + if (calendarView.isMultiSelectDayEnabled()) { + calendarView.setOnMultipleDaySelectedListener((month, dates) -> { + //Do something with your current selection + }); + } - calendarView.setFirstDayOfWeek(Calendar.MONDAY); - calendarView.setIsOverflowDateVisible(true); calendarView.update(Calendar.getInstance(Locale.getDefault())); - calendarView.setOnDateLongClickListener(selectedDate -> textView.setText(formatter.format(selectedDate))); - calendarView.setOnMonthChangeListener(monthDate -> { - final SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy", Locale.getDefault()); - - if (null != actionBar) { - String dateStr = df.format(monthDate); - dateStr = dateStr.substring(0, 1).toUpperCase() + dateStr.substring(1, dateStr.length()); - - actionBar.setTitle(dateStr); - } - }); } @Override @@ -131,4 +126,31 @@ public void animateViews() { animate(fab, getApplicationContext()); animate(textView, getApplicationContext()); } + + private void onDateLongClick(@NonNull final Date date) { + textView.setText(formatDate(DATE_TEMPLATE, date)); + } + + private void onDateClick(@NonNull final Date date) { + textView.setText(formatDate(DATE_TEMPLATE, date)); + } + + private void onMonthTitleClick(@NonNull final Date date) { + //Do something after month selection + } + + private void onMonthChange(@NonNull final Date date) { + final ActionBar actionBar = getSupportActionBar(); + + if (null != actionBar) { + String dateStr = formatDate(MONTH_TEMPLATE, date); + dateStr = dateStr.substring(0, 1).toUpperCase() + dateStr.substring(1, dateStr.length()); + + actionBar.setTitle(dateStr); + } + } + + private String formatDate(@NonNull String dateTemplate, @NonNull Date date) { + return new SimpleDateFormat(dateTemplate, Locale.getDefault()).format(date); + } } diff --git a/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java b/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java index 5577068..d097304 100644 --- a/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java +++ b/materialcalendarview/src/main/java/io/blackbox_vision/materialcalendarview/view/CalendarView.java @@ -8,6 +8,7 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Build; +import android.os.Parcelable; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; @@ -190,7 +191,6 @@ public final class CalendarView extends LinearLayout { private int backButtonDrawable; private int nextButtonDrawable; private boolean isOverflowDateVisible; - private boolean isMultiSelectDayEnabled; private int firstDayOfWeek; @@ -231,6 +231,16 @@ public CalendarView(Context context, AttributeSet attrs) { drawCalendar(); } + @Override + protected Parcelable onSaveInstanceState() { + return super.onSaveInstanceState(); + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + super.onRestoreInstanceState(state); + } + private void initTouchVariables() { final ViewConfiguration configuration = ViewConfiguration.get(getContext()); final float density = getContext().getResources().getDisplayMetrics().density; @@ -1268,4 +1278,12 @@ public CalendarView setMultiSelectDayEnabled(boolean multiSelectDayEnabled) { invalidate(); return this; } + + public boolean isOverflowDateVisible() { + return isOverflowDateVisible; + } + + public boolean isMultiSelectDayEnabled() { + return isMultiSelectDayEnabled; + } }