Skip to content

Commit

Permalink
Theming MonthView
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlo Marinangeli committed Feb 10, 2017
1 parent 7e156fb commit 3f37248
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 53 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -203,32 +205,28 @@ 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);

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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String, Integer> params) {
if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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)
Expand Down Expand Up @@ -606,30 +604,30 @@ 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
*/
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);
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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)) {
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="mdtp_theme_dark" format="boolean" />
<attr name="mdtp_monthViewTheme" format="reference" />

<declare-styleable name="MonthView">
<attr name="dayTextColor" format="color" />
<attr name="dayHighlightedTextColor" format="color" />
<attr name="dayDisabledTextColor" format="color" />
<attr name="monthDayTextColor" format="color" />
<attr name="miniDayNumberTextSize" format="dimension" />
<attr name="monthLabelTextSize" format="dimension" />
<attr name="monthDayTextSize" format="dimension" />
<attr name="monthHeaderSize" format="dimension" />
<attr name="daySelectedCircleSize" format="dimension" />
</declare-styleable>

</resources>
3 changes: 1 addition & 2 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
23 changes: 20 additions & 3 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,7 +14,25 @@
-->

<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<attr name="mdtp_theme_dark" format="boolean">false</attr>

<style name="MonthView">
<item name="dayTextColor">@color/mdtp_date_picker_text_normal</item>
<item name="dayHighlightedTextColor">@color/mdtp_date_picker_text_highlighted</item>
<item name="dayDisabledTextColor">@color/mdtp_date_picker_text_disabled</item>
<item name="monthDayTextColor">@color/mdtp_date_picker_month_day</item>
<item name="miniDayNumberTextSize">@dimen/mdtp_day_number_size</item>
<item name="monthLabelTextSize">@dimen/mdtp_month_label_size</item>
<item name="monthDayTextSize">@dimen/mdtp_month_day_label_text_size</item>
<item name="monthHeaderSize">@dimen/mdtp_month_list_item_header_height</item>
<item name="daySelectedCircleSize">@dimen/mdtp_day_number_select_circle_radius</item>
</style>

<style name="MonthView.Dark">
<item name="dayTextColor">@color/mdtp_date_picker_text_normal_dark_theme</item>
<item name="dayDisabledTextColor">@color/mdtp_date_picker_text_disabled_dark_theme</item>
<item name="dayHighlightedTextColor">@color/mdtp_date_picker_text_highlighted_dark_theme</item>
<item name="monthDayTextColor">@color/mdtp_date_picker_month_day_dark_theme</item>
</style>

<style name="mdtp_time_label">
<item name="android:textSize">@dimen/mdtp_time_label_size</item>
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<item name="colorAccent">#ff4081</item>
<!-- Customize picker mode here. -->
<item name="mdtp_theme_dark">true</item>
<item name="mdtp_monthViewTheme">@style/MonthView</item>
</style>

</resources>

0 comments on commit 3f37248

Please sign in to comment.