Tint selectableItemBackground with accent color for versions below 21

This commit is contained in:
Phillip Hsu 2016-08-24 22:24:49 -07:00
parent f2e25cb261
commit fd57663b86
3 changed files with 26 additions and 7 deletions

View File

@ -20,10 +20,10 @@ import android.animation.Keyframe;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder; import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable; import android.graphics.drawable.RippleDrawable;
import android.os.Build; import android.os.Build;
@ -247,16 +247,24 @@ public class Utils {
} }
/** /**
* Sets the color on the {@code view}'s RippleDrawable background, * Sets the color on the {@code view}'s {@code selectableItemBackground} or the
* if its background was set to {@code android.R.attr.selectableItemBackground} or * borderless variant, whichever was set as the background.
* {@code android.R.attr.selectableItemBackgroundBorderless}.
* @param view the view that should have its highlight color changed * @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) { public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
Drawable selectableItemBackground = view.getBackground(); Drawable selectableItemBackground = view.getBackground();
if (selectableItemBackground instanceof RippleDrawable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& selectableItemBackground instanceof RippleDrawable) {
((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color)); ((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
} else {
// Draws the color (src) onto the background (dest) *in the same plane*.
// That means the color is not overlapping (i.e. on a higher z-plane, covering)
// the background. That would be done with SRC_OVER.
// The DrawableCompat tinting APIs *could* be a viable alternative, if you
// call setTintMode(). Previous attempts using those APIs failed without
// the tint mode. However, those APIs have the overhead of mutating and wrapping
// the drawable.
selectableItemBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
} }
} }

View File

@ -6,4 +6,15 @@
<item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item> <item name="android:statusBarColor">@android:color/transparent</item>
</style> </style>
<style name="GridLayoutNumpadElement">
<!-- http://stackoverflow.com/a/6868308/5055032
Leave off the namespace to reference a custom attribute.
Here, we are referencing the "grid:" namespace -->
<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/selectableItemBackgroundBorderless</item>
<item name="android:gravity">center</item>
</style>
</resources> </resources>

View File

@ -37,7 +37,7 @@
<item name="layout_columnWeight">1</item> <item name="layout_columnWeight">1</item>
<!--<item name="layout_rowWeight">1</item>--> <!--<item name="layout_rowWeight">1</item>-->
<item name="android:layout_height">@dimen/numeric_keypad_cell_height</item> <item name="android:layout_height">@dimen/numeric_keypad_cell_height</item>
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item> <item name="android:background">?android:attr/selectableItemBackground</item>
<item name="android:gravity">center</item> <item name="android:gravity">center</item>
</style> </style>