Tint selectableItemBackground with accent color

This commit is contained in:
Phillip Hsu 2016-08-24 19:43:24 -07:00
parent 29086b4b02
commit f2e25cb261
8 changed files with 64 additions and 19 deletions

View File

@ -20,10 +20,12 @@ import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
@ -55,7 +57,6 @@ public class Utils {
// Alpha level for fully opaque.
public static final int FULL_ALPHA = 255;
static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
public static boolean isJellybeanOrLater() {
@ -245,6 +246,20 @@ public class Utils {
return d;
}
/**
* Sets the color on the {@code view}'s RippleDrawable background,
* if its background was set to {@code android.R.attr.selectableItemBackground} or
* {@code android.R.attr.selectableItemBackgroundBorderless}.
* @param view the view that should have its highlight color changed
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
Drawable selectableItemBackground = view.getBackground();
if (selectableItemBackground instanceof RippleDrawable) {
((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
}
}
/**
* Gets dialog type (Light/Dark) from current theme
* @param context The context to use as reference for the boolean

View File

@ -11,6 +11,7 @@ import android.view.View;
import android.widget.TextView;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.aospdatetimepicker.Utils;
import java.util.Arrays;
@ -39,7 +40,8 @@ public abstract class GridLayoutNumpad extends GridLayout {
private int mCount = 0;
private OnInputChangeListener mOnInputChangeListener;
ColorStateList mColors;
private ColorStateList mTextColors;
int mAccentColor;
@Bind({ R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
@ -63,8 +65,7 @@ public abstract class GridLayoutNumpad extends GridLayout {
}
public GridLayoutNumpad(Context context) {
super(context);
init();
this(context, null);
}
public GridLayoutNumpad(Context context, AttributeSet attrs) {
@ -83,13 +84,29 @@ public abstract class GridLayoutNumpad extends GridLayout {
// The buttons are actually of type Button, but we kept references
// to them as TextViews... which is fine since TextView is the superclass
// of Button.
mColors = ContextCompat.getColorStateList(context,
themeDark? R.color.numeric_keypad_button_text_dark : R.color.numeric_keypad_button_text);
mTextColors = ContextCompat.getColorStateList(context, themeDark?
R.color.numeric_keypad_button_text_dark : R.color.numeric_keypad_button_text);
// AFAIK, the only way to get the user's accent color is programmatically,
// because it is uniquely defined in their app's theme. It is not possible
// for us to reference that via XML (i.e. with ?colorAccent or similar),
// which happens at compile time.
// TOneverDO: Use any other Context to retrieve the accent color. We must use
// the Context param passed to us, because we know this context to be
// NumpadTimePickerDialog.getContext(), which is equivalent to
// NumpadTimePickerDialog.getActivity(). It is from that Activity where we
// get its theme's colorAccent.
mAccentColor = Utils.getThemeAccentColor(context);
for (TextView b : mButtons) {
b.setTextColor(mColors);
setTextColor(b);
Utils.setColorControlHighlight(b, mAccentColor);
}
}
void setTextColor(TextView view) {
view.setTextColor(mTextColors);
}
/**
* @return the number of digits we can input
*/

View File

@ -503,6 +503,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
mHalfDayToggleUnselectedColor = ContextCompat.getColor(getContext(),
mThemeDark? R.color.text_color_primary_dark : R.color.text_color_primary_light);
Utils.setColorControlHighlight(mLeftHalfDayToggle, accentColor);
Utils.setColorControlHighlight(mRightHalfDayToggle, accentColor);
// Update the half day at the end when the state colors have been initialized
updateHalfDay(mInitialHourOfDay < 12? AM : PM);
return view;

View File

@ -22,7 +22,7 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
OnNumberSelectedListener mSelectionListener;
View mLastSelectedView;
private final int mSelectedTextColor;
final int mSelectedTextColor;
// TODO: The half-day buttons in the dialog's layout also need to use this color.
// Consider moving this to either the Dialog class, or move the buttons and the FAB
// to the GridSelectorLayout class and then move these to GridSelectorLayout.
@ -48,6 +48,10 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
mIsInitialized = false;
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, android.R.attr.textColorPrimary);
mDefaultTextColor = ContextCompat.getColor(context, R.color.text_color_primary_light);
// The reason we can use the Context passed here and get the correct accent color
// is that this NumbersGrid is programmatically created by the GridSelectorLayout in
// its initialize(), and the Context passed in there is from
// NumberGridTimePickerDialog.getActivity().
mSelectedTextColor = Utils.getThemeAccentColor(context);
// Show the first button as default selected
setIndicator(getChildAt(indexOfDefaultValue()));
@ -142,6 +146,7 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
R.color.text_color_primary_dark : R.color.text_color_primary_light);
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
Utils.setColorControlHighlight(v, mSelectedTextColor/*colorAccent*/);
// Filter out views that aren't number buttons
if (canRegisterClickListener(v)) {
((TextView) v).setTextColor(mDefaultTextColor);

View File

@ -63,7 +63,6 @@ public class NumpadTimePicker extends GridLayoutNumpad {
@Bind(R.id.backspace) ImageButton mBackspace;
private boolean mThemeDark;
private int mAccentColor;
private int mFabDisabledColorDark;
private int mFabDisabledColorLight;
@ -105,12 +104,20 @@ public class NumpadTimePicker extends GridLayoutNumpad {
void setTheme(Context context, boolean themeDark) {
super.setTheme(context, themeDark);
mThemeDark = themeDark;
// this.getContext() ==> default teal accent color
// application context ==> white
// The Context that was passed in is NumpadTimePickerDialog.getContext() which
// is probably the host Activity. I have no idea what this.getContext() returns,
// but its probably some internal type that isn't tied to any of our application
// components.
// So, we kept the 0-9 buttons as TextViews, but here we kept
// the alt buttons as actual Buttons...
for (Button b : mAltButtons) {
b.setTextColor(mColors);
setTextColor(b);
Utils.setColorControlHighlight(b, mAccentColor);
}
Utils.setColorControlHighlight(mBackspace, mAccentColor);
ColorStateList colorBackspace = ContextCompat.getColorStateList(context,
themeDark? R.color.icon_color_dark : R.color.icon_color);
@ -120,13 +127,6 @@ public class NumpadTimePicker extends GridLayoutNumpad {
themeDark? R.color.icon_color_dark : R.color.fab_icon_color);
Utils.setTintList(mFab, mFab.getDrawable(), colorIcon);
// this.getContext() ==> default teal accent color
// application context ==> white
// The Context that was passed in is NumpadTimePickerDialog.getContext() which
// is probably the host Activity. I have no idea what this.getContext() returns,
// but its probably some internal type that isn't tied to any of our application
// components.
mAccentColor = Utils.getThemeAccentColor(context);
// Make sure the dark theme disabled color shows up initially
updateFabState();
}

View File

@ -59,7 +59,7 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
// TODO: Do these in initialize()
ret.setOnTimeSetListener(callback);
ret.mThemeDark = true;
ret.mThemeDark = false;
return ret;
}

View File

@ -5,6 +5,7 @@ import android.support.v4.content.ContextCompat;
import android.view.View;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.aospdatetimepicker.Utils;
/**
* Created by Phillip Hsu on 8/17/2016.
@ -84,6 +85,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
R.color.text_color_secondary_dark : R.color.text_color_secondary_light);
for (int i = 0; i < getChildCount(); i++) {
TwentyFourHourGridItem item = (TwentyFourHourGridItem) getChildAt(i);
Utils.setColorControlHighlight(item, mSelectedTextColor/*colorAccent*/);
item.getPrimaryTextView().setTextColor(mDefaultTextColor);
item.getSecondaryTextView().setTextColor(mSecondaryTextColor);
}

View File

@ -37,7 +37,7 @@
<item name="layout_columnWeight">1</item>
<!--<item name="layout_rowWeight">1</item>-->
<item name="android:layout_height">@dimen/numeric_keypad_cell_height</item>
<item name="android:background">?android:attr/selectableItemBackground</item>
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
<item name="android:gravity">center</item>
</style>
@ -122,6 +122,9 @@
</style>
<style name="ModalStyle" parent="Widget.Design.BottomSheet.Modal">
<!-- TODO: See if this can be set programmatically, or else we'd have to force users
~ to declare a bottomSheetDialogTheme in their theme.
-->
<item name="behavior_peekHeight">@dimen/peek_height_upper_limit</item>
</style>