diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPager.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPager.java index 6f9072ad..33562555 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPager.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/CalendarPager.java @@ -1,7 +1,10 @@ package com.prolificinteractive.materialcalendarview; import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; +import android.util.AttributeSet; import android.view.MotionEvent; /** @@ -11,10 +14,14 @@ class CalendarPager extends ViewPager { private boolean pagingEnabled = true; - public CalendarPager(Context context) { + public CalendarPager(@NonNull final Context context) { super(context); } + public CalendarPager(@NonNull final Context context, @Nullable final AttributeSet attrs) { + super(context, attrs); + } + /** * enable disable viewpager scroll * diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/DirectionButton.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/DirectionButton.java deleted file mode 100644 index 0a96ca02..00000000 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/DirectionButton.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.prolificinteractive.materialcalendarview; - -import android.content.Context; -import android.os.Build; -import android.support.v7.widget.AppCompatImageView; -import android.util.TypedValue; - -/** - * An {@linkplain android.widget.ImageView} to pragmatically set the color of arrows - * using a {@linkplain android.graphics.ColorFilter} - */ -class DirectionButton extends AppCompatImageView { - - public DirectionButton(Context context) { - super(context); - - setBackgroundResource(getThemeSelectableBackgroundId(context)); - } - - @Override - public void setEnabled(boolean enabled) { - super.setEnabled(enabled); - setAlpha(enabled ? 1f : 0.1f); - } - - private static int getThemeSelectableBackgroundId(Context context) { - //Get selectableItemBackgroundBorderless defined for AppCompat - int colorAttr = context.getResources().getIdentifier( - "selectableItemBackgroundBorderless", "attr", context.getPackageName()); - - if (colorAttr == 0) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - colorAttr = android.R.attr.selectableItemBackgroundBorderless; - } else { - colorAttr = android.R.attr.selectableItemBackground; - } - } - - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(colorAttr, outValue, true); - return outValue.resourceId; - } -} diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java index 23b688f8..6205f2a5 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java @@ -1,6 +1,7 @@ package com.prolificinteractive.materialcalendarview; import android.annotation.SuppressLint; +import android.app.Service; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; @@ -14,11 +15,10 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; -import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; import android.util.SparseArray; import android.util.TypedValue; -import android.view.Gravity; +import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -175,8 +175,8 @@ public class MaterialCalendarView extends ViewGroup { private final TitleChanger titleChanger; private final TextView title; - private final DirectionButton buttonPast; - private final DirectionButton buttonFuture; + private final ImageView buttonPast; + private final ImageView buttonFuture; private final CalendarPager pager; private CalendarPagerAdapter adapter; private CalendarDay currentMonth; @@ -257,11 +257,14 @@ public MaterialCalendarView(Context context, AttributeSet attrs) { setClipToPadding(true); } - buttonPast = new DirectionButton(getContext()); - buttonPast.setContentDescription(getContext().getString(R.string.previous)); - title = new AppCompatTextView(getContext()); - buttonFuture = new DirectionButton(getContext()); - buttonFuture.setContentDescription(getContext().getString(R.string.next)); + final LayoutInflater inflater = + (LayoutInflater) getContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE); + final View content = inflater.inflate(R.layout.calendar_view, null, false); + + topbar = content.findViewById(R.id.header); + buttonPast = content.findViewById(R.id.previous); + title = content.findViewById(R.id.month_name); + buttonFuture = content.findViewById(R.id.next); pager = new CalendarPager(getContext()); buttonPast.setOnClickListener(onClickListener); @@ -415,22 +418,7 @@ public void transformPage(View page, float position) { } private void setupChildren() { - topbar = new LinearLayout(getContext()); - topbar.setOrientation(LinearLayout.HORIZONTAL); - topbar.setClipChildren(false); - topbar.setClipToPadding(false); - addView(topbar, new LayoutParams(1)); - - buttonPast.setScaleType(ImageView.ScaleType.CENTER_INSIDE); - topbar.addView(buttonPast, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1)); - - title.setGravity(Gravity.CENTER); - topbar.addView(title, new LinearLayout.LayoutParams( - 0, LayoutParams.MATCH_PARENT, DEFAULT_DAYS_IN_WEEK - 2 - )); - - buttonFuture.setScaleType(ImageView.ScaleType.CENTER_INSIDE); - topbar.addView(buttonFuture, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1)); + addView(topbar); pager.setId(R.id.mcv_pager); pager.setOffscreenPageLimit(1); @@ -441,8 +429,8 @@ private void setupChildren() { private void updateUi() { titleChanger.change(currentMonth); - buttonPast.setEnabled(canGoBack()); - buttonFuture.setEnabled(canGoForward()); + enableView(buttonPast, canGoBack()); + enableView(buttonFuture, canGoForward()); } /** @@ -2008,4 +1996,15 @@ private void commit(State state) { invalidateDecorators(); updateUi(); } + + /** + * Used for enabling or disabling views, while also changing the alpha. + * + * @param view The view to enable or disable. + * @param enable Whether to enable or disable the view. + */ + private static void enableView(final View view, final boolean enable) { + view.setEnabled(enable); + view.setAlpha(enable ? 1f : 0.1f); + } } diff --git a/library/src/main/res/layout/calendar_view.xml b/library/src/main/res/layout/calendar_view.xml new file mode 100644 index 00000000..4d8a2420 --- /dev/null +++ b/library/src/main/res/layout/calendar_view.xml @@ -0,0 +1,45 @@ + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/dimens.xml b/library/src/main/res/values/dimens.xml new file mode 100644 index 00000000..69a44add --- /dev/null +++ b/library/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 4dp + 4dp +