Skip to content

Commit fe61a6d

Browse files
author
Carlo Marinangeli
committed
Theming MonthView
1 parent 7e156fb commit fe61a6d

5 files changed

Lines changed: 86 additions & 53 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0-beta2'
8+
classpath 'com.android.tools.build:gradle:2.3.0-beta4'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

library/src/main/java/com/wdullaer/materialdatetimepicker/date/MonthView.java

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import android.content.Context;
2020
import android.content.res.Resources;
21+
import android.content.res.TypedArray;
2122
import android.graphics.Canvas;
23+
import android.graphics.Color;
2224
import android.graphics.Paint;
2325
import android.graphics.Paint.Align;
2426
import android.graphics.Paint.Style;
@@ -184,9 +186,9 @@ public abstract class MonthView extends View {
184186
protected int mSelectedDayTextColor;
185187
protected int mMonthDayTextColor;
186188
protected int mTodayNumberColor;
189+
protected int mMonthTitleColor;
187190
protected int mHighlightedDayTextColor;
188191
protected int mDisabledDayTextColor;
189-
protected int mMonthTitleColor;
190192

191193
public MonthView(Context context) {
192194
this(context, null, null);
@@ -203,32 +205,28 @@ public MonthView(Context context, AttributeSet attr, DatePickerController contro
203205
mDayOfWeekTypeface = res.getString(R.string.mdtp_day_of_week_label_typeface);
204206
mMonthTitleTypeface = res.getString(R.string.mdtp_sans_serif);
205207

206-
boolean darkTheme = mController != null && mController.isThemeDark();
207-
if(darkTheme) {
208-
mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal_dark_theme);
209-
mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day_dark_theme);
210-
mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled_dark_theme);
211-
mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted_dark_theme);
212-
}
213-
else {
214-
mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal);
215-
mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day);
216-
mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled);
217-
mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted);
218-
}
208+
TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.MonthView, R.attr.mdtp_monthViewTheme, R.style.MonthView);
209+
210+
mDayTextColor = typedArray.getColor(R.styleable.MonthView_dayTextColor, Color.BLACK);
211+
mHighlightedDayTextColor = typedArray.getColor(R.styleable.MonthView_dayHighlightedTextColor, Color.BLACK);
212+
mDisabledDayTextColor = typedArray.getColor(R.styleable.MonthView_dayDisabledTextColor, Color.BLACK);
213+
mMonthDayTextColor = typedArray.getColor(R.styleable.MonthView_monthDayTextColor, Color.BLACK);
214+
215+
MINI_DAY_NUMBER_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_miniDayNumberTextSize, 0);
216+
MONTH_LABEL_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_monthLabelTextSize, 0);
217+
MONTH_DAY_LABEL_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_monthDayTextSize, 0);
218+
MONTH_HEADER_SIZE = typedArray.getDimensionPixelOffset(R.styleable.MonthView_monthHeaderSize, 0);
219+
DAY_SELECTED_CIRCLE_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthView_daySelectedCircleSize, 0);
220+
221+
typedArray.recycle();
222+
219223
mSelectedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_white);
220224
mTodayNumberColor = mController.getAccentColor();
221225
mMonthTitleColor = ContextCompat.getColor(context, R.color.mdtp_white);
222226

223227
mStringBuilder = new StringBuilder(50);
224228
mFormatter = new Formatter(mStringBuilder, Locale.getDefault());
225229

226-
MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size);
227-
MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size);
228-
MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size);
229-
MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height);
230-
DAY_SELECTED_CIRCLE_SIZE = res
231-
.getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius);
232230

233231
mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height)
234232
- getMonthHeaderSize()) / MAX_NUM_ROWS;
@@ -312,7 +310,7 @@ protected void initView() {
312310
mMonthDayLabelPaint.setAntiAlias(true);
313311
mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE);
314312
mMonthDayLabelPaint.setColor(mMonthDayTextColor);
315-
mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(),"Roboto-Medium"));
313+
mMonthDayLabelPaint.setTypeface(TypefaceHelper.get(getContext(), "Roboto-Medium"));
316314
mMonthDayLabelPaint.setStyle(Style.FILL);
317315
mMonthDayLabelPaint.setTextAlign(Align.CENTER);
318316
mMonthDayLabelPaint.setFakeBoldText(true);
@@ -342,7 +340,7 @@ protected void onDraw(Canvas canvas) {
342340
* {@link #VIEW_PARAMS_HEIGHT} for more info on parameters.
343341
*
344342
* @param params A map of the new parameters, see
345-
* {@link #VIEW_PARAMS_HEIGHT}
343+
* {@link #VIEW_PARAMS_HEIGHT}
346344
*/
347345
public void setMonthParams(HashMap<String, Integer> params) {
348346
if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) {
@@ -452,7 +450,7 @@ private String getMonthAndYearString() {
452450
Locale locale = Locale.getDefault();
453451
String pattern = "MMMM yyyy";
454452

455-
if(Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
453+
if (Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
456454
else pattern = DateFormat.getBestDateTimePattern(locale, pattern);
457455

458456
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
@@ -493,14 +491,14 @@ protected void drawMonthNums(Canvas canvas) {
493491
final float dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2.0f);
494492
int j = findDayOffset();
495493
for (int dayNumber = 1; dayNumber <= mNumCells; dayNumber++) {
496-
final int x = (int)((2 * j + 1) * dayWidthHalf + mEdgePadding);
494+
final int x = (int) ((2 * j + 1) * dayWidthHalf + mEdgePadding);
497495

498496
int yRelativeToDay = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH;
499497

500-
final int startX = (int)(x - dayWidthHalf);
501-
final int stopX = (int)(x + dayWidthHalf);
502-
final int startY = (int)(y - yRelativeToDay);
503-
final int stopY = (int)(startY + mRowHeight);
498+
final int startX = (int) (x - dayWidthHalf);
499+
final int stopX = (int) (x + dayWidthHalf);
500+
final int startY = (int) (y - yRelativeToDay);
501+
final int stopY = (int) (startY + mRowHeight);
504502

505503
drawMonthDay(canvas, mYear, mMonth, dayNumber, x, y, startX, stopX, startY, stopY);
506504

@@ -515,19 +513,19 @@ protected void drawMonthNums(Canvas canvas) {
515513
/**
516514
* This method should draw the month day. Implemented by sub-classes to allow customization.
517515
*
518-
* @param canvas The canvas to draw on
519-
* @param year The year of this month day
516+
* @param canvas The canvas to draw on
517+
* @param year The year of this month day
520518
* @param month The month of this month day
521-
* @param day The day number of this month day
522-
* @param x The default x position to draw the day number
523-
* @param y The default y position to draw the day number
524-
* @param startX The left boundary of the day number rect
519+
* @param day The day number of this month day
520+
* @param x The default x position to draw the day number
521+
* @param y The default y position to draw the day number
522+
* @param startX The left boundary of the day number rect
525523
* @param stopX The right boundary of the day number rect
526-
* @param startY The top boundary of the day number rect
524+
* @param startY The top boundary of the day number rect
527525
* @param stopY The bottom boundary of the day number rect
528526
*/
529527
public abstract void drawMonthDay(Canvas canvas, int year, int month, int day,
530-
int x, int y, int startX, int stopX, int startY, int stopY);
528+
int x, int y, int startX, int stopX, int startY, int stopY);
531529

532530
protected int findDayOffset() {
533531
return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart)
@@ -606,30 +604,30 @@ protected boolean isHighlighted(int year, int month, int day) {
606604

607605
/**
608606
* Return a 1 or 2 letter String for use as a weekday label
607+
*
609608
* @param day The day for which to generate a label
610609
* @return The weekday label
611610
*/
612611
private String getWeekDayLabel(Calendar day) {
613612
Locale locale = Locale.getDefault();
614613

615614
// Localised short version of the string is not available on API < 18
616-
if(Build.VERSION.SDK_INT < 18) {
615+
if (Build.VERSION.SDK_INT < 18) {
617616
String dayName = new SimpleDateFormat("E", locale).format(day.getTime());
618617
String dayLabel = dayName.toUpperCase(locale).substring(0, 1);
619618

620619
// Chinese labels should be fetched right to left
621620
if (locale.equals(Locale.CHINA) || locale.equals(Locale.CHINESE) || locale.equals(Locale.SIMPLIFIED_CHINESE) || locale.equals(Locale.TRADITIONAL_CHINESE)) {
622621
int len = dayName.length();
623-
dayLabel = dayName.substring(len -1, len);
622+
dayLabel = dayName.substring(len - 1, len);
624623
}
625624

626625
// Most hebrew labels should select the second to last character
627626
if (locale.getLanguage().equals("he") || locale.getLanguage().equals("iw")) {
628-
if(mDayLabelCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
627+
if (mDayLabelCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
629628
int len = dayName.length();
630629
dayLabel = dayName.substring(len - 2, len - 1);
631-
}
632-
else {
630+
} else {
633631
// I know this is duplication, but it makes the code easier to grok by
634632
// having all hebrew code in the same block
635633
dayLabel = dayName.toUpperCase(locale).substring(0, 1);
@@ -638,7 +636,7 @@ private String getWeekDayLabel(Calendar day) {
638636

639637
// Catalan labels should be two digits in lowercase
640638
if (locale.getLanguage().equals("ca"))
641-
dayLabel = dayName.toLowerCase().substring(0,2);
639+
dayLabel = dayName.toLowerCase().substring(0, 2);
642640

643641
// Correct single character label in Spanish is X
644642
if (locale.getLanguage().equals("es") && day.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY)
@@ -652,7 +650,7 @@ private String getWeekDayLabel(Calendar day) {
652650

653651
/**
654652
* @return The date that has accessibility focus, or {@code null} if no date
655-
* has focus
653+
* has focus
656654
*/
657655
public CalendarDay getAccessibilityFocus() {
658656
final int day = mTouchHelper.getFocusedVirtualView();
@@ -675,7 +673,7 @@ public void clearAccessibilityFocus() {
675673
*
676674
* @param day The date which should receive focus
677675
* @return {@code false} if the date is not valid for this month view, or
678-
* {@code true} if the date received focus
676+
* {@code true} if the date received focus
679677
*/
680678
public boolean restoreAccessibilityFocus(CalendarDay day) {
681679
if ((day.year != mYear) || (day.month != mMonth) || (day.day > mNumCells)) {
@@ -737,7 +735,7 @@ protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEve
737735

738736
@Override
739737
protected void onPopulateNodeForVirtualView(int virtualViewId,
740-
AccessibilityNodeInfoCompat node) {
738+
AccessibilityNodeInfoCompat node) {
741739
getItemBounds(virtualViewId, mTempRect);
742740

743741
node.setContentDescription(getItemDescription(virtualViewId));
@@ -752,7 +750,7 @@ protected void onPopulateNodeForVirtualView(int virtualViewId,
752750

753751
@Override
754752
protected boolean onPerformActionForVirtualView(int virtualViewId, int action,
755-
Bundle arguments) {
753+
Bundle arguments) {
756754
switch (action) {
757755
case AccessibilityNodeInfo.ACTION_CLICK:
758756
onDayClick(virtualViewId);
@@ -765,7 +763,7 @@ protected boolean onPerformActionForVirtualView(int virtualViewId, int action,
765763
/**
766764
* Calculates the bounding rectangle of a given time object.
767765
*
768-
* @param day The day to calculate bounds for
766+
* @param day The day to calculate bounds for
769767
* @param rect The rectangle in which to store the bounds
770768
*/
771769
protected void getItemBounds(int day, Rect rect) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<attr name="mdtp_theme_dark" format="boolean" />
5+
<attr name="mdtp_monthViewTheme" format="reference" />
6+
7+
<declare-styleable name="MonthView">
8+
<attr name="dayTextColor" format="color" />
9+
<attr name="dayHighlightedTextColor" format="color" />
10+
<attr name="dayDisabledTextColor" format="color" />
11+
<attr name="monthDayTextColor" format="color" />
12+
<attr name="miniDayNumberTextSize" format="dimension" />
13+
<attr name="monthLabelTextSize" format="dimension" />
14+
<attr name="monthDayTextSize" format="dimension" />
15+
<attr name="monthHeaderSize" format="dimension" />
16+
<attr name="daySelectedCircleSize" format="dimension" />
17+
</declare-styleable>
18+
19+
</resources>

library/src/main/res/values/colors.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
Copyright (C) 2013 The Android Open Source Project
43
54
Licensed under the Apache License, Version 2.0 (the "License");

library/src/main/res/values/styles.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!-- Copyright (C) 2013 The Android Open Source Project
1+
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2013 The Android Open Source Project
32
43
Licensed under the Apache License, Version 2.0 (the "License");
54
you may not use this file except in compliance with the License.
@@ -15,7 +14,25 @@
1514
-->
1615

1716
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
18-
<attr name="mdtp_theme_dark" format="boolean">false</attr>
17+
18+
<style name="MonthView">
19+
<item name="dayTextColor">@color/mdtp_date_picker_text_normal</item>
20+
<item name="dayHighlightedTextColor">@color/mdtp_date_picker_text_highlighted</item>
21+
<item name="dayDisabledTextColor">@color/mdtp_date_picker_text_disabled</item>
22+
<item name="monthDayTextColor">@color/mdtp_date_picker_month_day</item>
23+
<item name="miniDayNumberTextSize">@dimen/mdtp_day_number_size</item>
24+
<item name="monthLabelTextSize">@dimen/mdtp_month_label_size</item>
25+
<item name="monthDayTextSize">@dimen/mdtp_month_day_label_text_size</item>
26+
<item name="monthHeaderSize">@dimen/mdtp_month_list_item_header_height</item>
27+
<item name="daySelectedCircleSize">@dimen/mdtp_day_number_select_circle_radius</item>
28+
</style>
29+
30+
<style name="MonthView.Dark">
31+
<item name="dayTextColor">@color/mdtp_date_picker_text_normal_dark_theme</item>
32+
<item name="dayDisabledTextColor">@color/mdtp_date_picker_text_disabled_dark_theme</item>
33+
<item name="dayHighlightedTextColor">@color/mdtp_date_picker_text_highlighted_dark_theme</item>
34+
<item name="monthDayTextColor">@color/mdtp_date_picker_month_day_dark_theme</item>
35+
</style>
1936

2037
<style name="mdtp_time_label">
2138
<item name="android:textSize">@dimen/mdtp_time_label_size</item>

0 commit comments

Comments
 (0)