Skip to content

Commit fadcb7f

Browse files
dsn5ftgsajith
authored andcommitted
Add a MaterialAttributes#resolveAttribute method that doesn't throw, a
MaterialColors#getColor method that allows for a default value, and a MaterialResources#getColorStateList that accepts a TintTypedArray PiperOrigin-RevId: 219170329
1 parent c3d6042 commit fadcb7f

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

lib/java/com/google/android/material/color/MaterialColors.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.support.annotation.RestrictTo;
2525
import com.google.android.material.resources.MaterialAttributes;
2626
import android.support.v4.graphics.ColorUtils;
27+
import android.util.TypedValue;
2728
import android.view.View;
2829

2930
/**
@@ -40,10 +41,31 @@ public class MaterialColors {
4041
public static final float ALPHA_LOW = 0.32F;
4142
public static final float ALPHA_DISABLED_LOW = 0.12F;
4243

44+
/**
45+
* Returns the color int for the provided theme color attribute, or throws an {@link
46+
* IllegalArgumentException} if the attribute is not set in the current theme.
47+
*/
48+
@ColorInt
4349
public static int getColor(View view, @AttrRes int colorAttributeResId) {
4450
return MaterialAttributes.resolveAttributeOrThrow(view, colorAttributeResId).data;
4551
}
4652

53+
/**
54+
* Returns the color int for the provided theme color attribute, or the default value if the
55+
* attribute is not set in the current theme.
56+
*/
57+
@ColorInt
58+
public static int getColor(
59+
View view, @AttrRes int colorAttributeResId, @ColorInt int defaultValue) {
60+
TypedValue typedValue =
61+
MaterialAttributes.resolveAttribute(view.getContext(), colorAttributeResId);
62+
if (typedValue != null) {
63+
return typedValue.data;
64+
} else {
65+
return defaultValue;
66+
}
67+
}
68+
4769
/**
4870
* Convenience method that calculates {@link MaterialColors#layer(View, int, int, float)} without
4971
* an {@code overlayAlpha} value by passing in {@code 1f} for the alpha value.

lib/java/com/google/android/material/resources/MaterialAttributes.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import android.content.Context;
2121
import android.support.annotation.AttrRes;
22+
import android.support.annotation.Nullable;
2223
import android.support.annotation.RestrictTo;
2324
import android.util.TypedValue;
2425
import android.view.View;
@@ -33,9 +34,9 @@ public class MaterialAttributes {
3334

3435
public static TypedValue resolveAttributeOrThrow(
3536
View componentView, @AttrRes int attributeResId) {
36-
TypedValue typedValue = new TypedValue();
3737
Context context = componentView.getContext();
38-
if (!context.getTheme().resolveAttribute(attributeResId, typedValue, true)) {
38+
TypedValue typedValue = resolveAttribute(context, attributeResId);
39+
if (typedValue == null) {
3940
String errorMessage =
4041
"The %1$s view requires a value for the %2$s attribute to be set in your app theme. "
4142
+ "You can either set the attribute in your theme or "
@@ -48,4 +49,13 @@ public static TypedValue resolveAttributeOrThrow(
4849
}
4950
return typedValue;
5051
}
52+
53+
@Nullable
54+
public static TypedValue resolveAttribute(Context context, @AttrRes int attributeResId) {
55+
TypedValue typedValue = new TypedValue();
56+
if (context.getTheme().resolveAttribute(attributeResId, typedValue, true)) {
57+
return typedValue;
58+
}
59+
return null;
60+
}
5161
}

lib/java/com/google/android/material/resources/MaterialResources.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.support.annotation.RestrictTo;
2929
import android.support.annotation.StyleableRes;
3030
import android.support.v7.content.res.AppCompatResources;
31+
import android.support.v7.widget.TintTypedArray;
3132

3233
/** Utility methods to resolve resources for components. */
3334
@RestrictTo(LIBRARY_GROUP)
@@ -36,8 +37,8 @@ public class MaterialResources {
3637
private MaterialResources() {}
3738

3839
/**
39-
* Returns the {@link ColorStateList} from the given attributes. The resource can include
40-
* themeable attributes, regardless of API level.
40+
* Returns the {@link ColorStateList} from the given {@link TypedArray} attributes. The resource
41+
* can include themeable attributes, regardless of API level.
4142
*/
4243
@Nullable
4344
public static ColorStateList getColorStateList(
@@ -64,6 +65,35 @@ public static ColorStateList getColorStateList(
6465
return attributes.getColorStateList(index);
6566
}
6667

68+
/**
69+
* Returns the {@link ColorStateList} from the given {@link TintTypedArray} attributes. The
70+
* resource can include themeable attributes, regardless of API level.
71+
*/
72+
@Nullable
73+
public static ColorStateList getColorStateList(
74+
Context context, TintTypedArray attributes, @StyleableRes int index) {
75+
if (attributes.hasValue(index)) {
76+
int resourceId = attributes.getResourceId(index, 0);
77+
if (resourceId != 0) {
78+
ColorStateList value = AppCompatResources.getColorStateList(context, resourceId);
79+
if (value != null) {
80+
return value;
81+
}
82+
}
83+
}
84+
85+
// Reading a single color with getColorStateList() on API 15 and below doesn't always correctly
86+
// read the value. Instead we'll first try to read the color directly here.
87+
if (VERSION.SDK_INT <= VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
88+
int color = attributes.getColor(index, -1);
89+
if (color != -1) {
90+
return ColorStateList.valueOf(color);
91+
}
92+
}
93+
94+
return attributes.getColorStateList(index);
95+
}
96+
6797
/**
6898
* Returns the drawable object from the given attributes.
6999
*

0 commit comments

Comments
 (0)