From 3f37248b7fb369c06432128f0d4dfe5f947e83d6 Mon Sep 17 00:00:00 2001 From: Carlo Marinangeli Date: Fri, 10 Feb 2017 12:38:59 +0000 Subject: [PATCH] Theming MonthView --- build.gradle | 2 +- .../date/MonthView.java | 92 +++++++++---------- library/src/main/res/values/attrs.xml | 19 ++++ library/src/main/res/values/colors.xml | 3 +- library/src/main/res/values/styles.xml | 23 ++++- sample/src/main/res/values/styles.xml | 1 + 6 files changed, 87 insertions(+), 53 deletions(-) create mode 100644 library/src/main/res/values/attrs.xml diff --git a/build.gradle b/build.gradle index 20361df4..16f9930c 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0-beta2' + classpath 'com.android.tools.build:gradle:2.3.0-beta4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/MonthView.java b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/MonthView.java index f98c06ce..597dfe99 100644 --- a/library/src/main/java/com/wdullaer/materialdatetimepicker/date/MonthView.java +++ b/library/src/main/java/com/wdullaer/materialdatetimepicker/date/MonthView.java @@ -18,7 +18,9 @@ import android.content.Context; import android.content.res.Resources; +import android.content.res.TypedArray; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Paint.Style; @@ -184,9 +186,9 @@ public abstract class MonthView extends View { protected int mSelectedDayTextColor; protected int mMonthDayTextColor; protected int mTodayNumberColor; + protected int mMonthTitleColor; protected int mHighlightedDayTextColor; protected int mDisabledDayTextColor; - protected int mMonthTitleColor; public MonthView(Context context) { this(context, null, null); @@ -203,19 +205,21 @@ public MonthView(Context context, AttributeSet attr, DatePickerController contro mDayOfWeekTypeface = res.getString(R.string.mdtp_day_of_week_label_typeface); mMonthTitleTypeface = res.getString(R.string.mdtp_sans_serif); - boolean darkTheme = mController != null && mController.isThemeDark(); - if(darkTheme) { - mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal_dark_theme); - mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day_dark_theme); - mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled_dark_theme); - mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted_dark_theme); - } - else { - mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal); - mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day); - mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled); - mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted); - } + TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.MonthView, R.attr.mdtp_monthViewTheme, R.style.MonthView); + + mDayTextColor = typedArray.getColor(R.styleable.MonthView_dayTextColor, Color.BLACK); + mHighlightedDayTextColor = typedArray.getColor(R.styleable.MonthView_dayHighlightedTextColor, Color.BLACK); + mDisabledDayTextColor = typedArray.getColor(R.styleable.MonthView_dayDisabledTextColor, Color.BLACK); + mMonthDayTextColor = typedArray.getColor(R.styleable.MonthView_monthDayTextColor, Color.BLACK); + + MINI_DAY_NUMBER_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_miniDayNumberTextSize, 0); + MONTH_LABEL_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_monthLabelTextSize, 0); + MONTH_DAY_LABEL_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_monthDayTextSize, 0); + MONTH_HEADER_SIZE = typedArray.getDimensionPixelOffset(R.styleable.MonthView_monthHeaderSize, 0); + DAY_SELECTED_CIRCLE_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_daySelectedCircleSize, 0); + + typedArray.recycle(); + mSelectedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_white); mTodayNumberColor = mController.getAccentColor(); mMonthTitleColor = ContextCompat.getColor(context, R.color.mdtp_white); @@ -223,12 +227,6 @@ public MonthView(Context context, AttributeSet attr, DatePickerController contro mStringBuilder = new StringBuilder(50); mFormatter = new Formatter(mStringBuilder, Locale.getDefault()); - MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size); - MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size); - MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size); - MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height); - DAY_SELECTED_CIRCLE_SIZE = res - .getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius); mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height) - getMonthHeaderSize()) / MAX_NUM_ROWS; @@ -312,7 +310,7 @@ protected void initView() { mMonthDayLabelPaint.setAntiAlias(true); mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE); mMonthDayLabelPaint.setColor(mMonthDayTextColor); - mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(),"Roboto-Medium")); + mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(), "Roboto-Medium")); mMonthDayLabelPaint.setStyle(Style.FILL); mMonthDayLabelPaint.setTextAlign(Align.CENTER); mMonthDayLabelPaint.setFakeBoldText(true); @@ -342,7 +340,7 @@ protected void onDraw(Canvas canvas) { * {@link #VIEW_PARAMS_HEIGHT} for more info on parameters. * * @param params A map of the new parameters, see - * {@link #VIEW_PARAMS_HEIGHT} + * {@link #VIEW_PARAMS_HEIGHT} */ public void setMonthParams(HashMap params) { if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) { @@ -452,7 +450,7 @@ private String getMonthAndYearString() { Locale locale = Locale.getDefault(); String pattern = "MMMM yyyy"; - if(Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear); + if (Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear); else pattern = DateFormat.getBestDateTimePattern(locale, pattern); SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale); @@ -493,14 +491,14 @@ protected void drawMonthNums(Canvas canvas) { final float dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2.0f); int j = findDayOffset(); for (int dayNumber = 1; dayNumber <= mNumCells; dayNumber++) { - final int x = (int)((2 * j + 1) * dayWidthHalf + mEdgePadding); + final int x = (int) ((2 * j + 1) * dayWidthHalf + mEdgePadding); int yRelativeToDay = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH; - final int startX = (int)(x - dayWidthHalf); - final int stopX = (int)(x + dayWidthHalf); - final int startY = (int)(y - yRelativeToDay); - final int stopY = (int)(startY + mRowHeight); + final int startX = (int) (x - dayWidthHalf); + final int stopX = (int) (x + dayWidthHalf); + final int startY = (int) (y - yRelativeToDay); + final int stopY = (int) (startY + mRowHeight); drawMonthDay(canvas, mYear, mMonth, dayNumber, x, y, startX, stopX, startY, stopY); @@ -515,19 +513,19 @@ protected void drawMonthNums(Canvas canvas) { /** * This method should draw the month day. Implemented by sub-classes to allow customization. * - * @param canvas The canvas to draw on - * @param year The year of this month day + * @param canvas The canvas to draw on + * @param year The year of this month day * @param month The month of this month day - * @param day The day number of this month day - * @param x The default x position to draw the day number - * @param y The default y position to draw the day number - * @param startX The left boundary of the day number rect + * @param day The day number of this month day + * @param x The default x position to draw the day number + * @param y The default y position to draw the day number + * @param startX The left boundary of the day number rect * @param stopX The right boundary of the day number rect - * @param startY The top boundary of the day number rect + * @param startY The top boundary of the day number rect * @param stopY The bottom boundary of the day number rect */ public abstract void drawMonthDay(Canvas canvas, int year, int month, int day, - int x, int y, int startX, int stopX, int startY, int stopY); + int x, int y, int startX, int stopX, int startY, int stopY); protected int findDayOffset() { return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart) @@ -606,6 +604,7 @@ protected boolean isHighlighted(int year, int month, int day) { /** * Return a 1 or 2 letter String for use as a weekday label + * * @param day The day for which to generate a label * @return The weekday label */ @@ -613,23 +612,22 @@ private String getWeekDayLabel(Calendar day) { Locale locale = Locale.getDefault(); // Localised short version of the string is not available on API < 18 - if(Build.VERSION.SDK_INT < 18) { + if (Build.VERSION.SDK_INT < 18) { String dayName = new SimpleDateFormat("E", locale).format(day.getTime()); String dayLabel = dayName.toUpperCase(locale).substring(0, 1); // Chinese labels should be fetched right to left if (locale.equals(Locale.CHINA) || locale.equals(Locale.CHINESE) || locale.equals(Locale.SIMPLIFIED_CHINESE) || locale.equals(Locale.TRADITIONAL_CHINESE)) { int len = dayName.length(); - dayLabel = dayName.substring(len -1, len); + dayLabel = dayName.substring(len - 1, len); } // Most hebrew labels should select the second to last character if (locale.getLanguage().equals("he") || locale.getLanguage().equals("iw")) { - if(mDayLabelCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) { + if (mDayLabelCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) { int len = dayName.length(); dayLabel = dayName.substring(len - 2, len - 1); - } - else { + } else { // I know this is duplication, but it makes the code easier to grok by // having all hebrew code in the same block dayLabel = dayName.toUpperCase(locale).substring(0, 1); @@ -638,7 +636,7 @@ private String getWeekDayLabel(Calendar day) { // Catalan labels should be two digits in lowercase if (locale.getLanguage().equals("ca")) - dayLabel = dayName.toLowerCase().substring(0,2); + dayLabel = dayName.toLowerCase().substring(0, 2); // Correct single character label in Spanish is X if (locale.getLanguage().equals("es") && day.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) @@ -652,7 +650,7 @@ private String getWeekDayLabel(Calendar day) { /** * @return The date that has accessibility focus, or {@code null} if no date - * has focus + * has focus */ public CalendarDay getAccessibilityFocus() { final int day = mTouchHelper.getFocusedVirtualView(); @@ -675,7 +673,7 @@ public void clearAccessibilityFocus() { * * @param day The date which should receive focus * @return {@code false} if the date is not valid for this month view, or - * {@code true} if the date received focus + * {@code true} if the date received focus */ public boolean restoreAccessibilityFocus(CalendarDay day) { if ((day.year != mYear) || (day.month != mMonth) || (day.day > mNumCells)) { @@ -737,7 +735,7 @@ protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEve @Override protected void onPopulateNodeForVirtualView(int virtualViewId, - AccessibilityNodeInfoCompat node) { + AccessibilityNodeInfoCompat node) { getItemBounds(virtualViewId, mTempRect); node.setContentDescription(getItemDescription(virtualViewId)); @@ -752,7 +750,7 @@ protected void onPopulateNodeForVirtualView(int virtualViewId, @Override protected boolean onPerformActionForVirtualView(int virtualViewId, int action, - Bundle arguments) { + Bundle arguments) { switch (action) { case AccessibilityNodeInfo.ACTION_CLICK: onDayClick(virtualViewId); @@ -765,7 +763,7 @@ protected boolean onPerformActionForVirtualView(int virtualViewId, int action, /** * Calculates the bounding rectangle of a given time object. * - * @param day The day to calculate bounds for + * @param day The day to calculate bounds for * @param rect The rectangle in which to store the bounds */ protected void getItemBounds(int day, Rect rect) { diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml new file mode 100644 index 00000000..d83df7cc --- /dev/null +++ b/library/src/main/res/values/attrs.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml index 5c8a5dcf..73042d13 100644 --- a/library/src/main/res/values/colors.xml +++ b/library/src/main/res/values/colors.xml @@ -1,5 +1,4 @@ - - - false + + + +