Skip to content

Commit b544530

Browse files
wcshiikim24
authored andcommitted
Update Chip's background / surface color state lists to support state_checked as well.
PiperOrigin-RevId: 240128475
1 parent 17195e4 commit b544530

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

lib/java/com/google/android/material/chip/ChipDrawable.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ public class ChipDrawable extends MaterialShapeDrawable
167167
private static final int[][] states =
168168
new int[][] {
169169
new int[] {
170-
android.R.attr.state_enabled, android.R.attr.state_selected
171-
}, // enabled and selected
172-
new int[] {android.R.attr.state_enabled}, // enabled
170+
android.R.attr.state_enabled, android.R.attr.state_selected,
171+
},
172+
new int[] {
173+
android.R.attr.state_enabled, android.R.attr.state_checked,
174+
},
175+
new int[] {android.R.attr.state_enabled},
173176
new int[] {} // default
174177
};
175178

@@ -1288,8 +1291,10 @@ private void updateCompatRippleColor() {
12881291
private void setChipSurfaceColor(@Nullable ColorStateList chipSurfaceColor) {
12891292
if (this.chipSurfaceColor != chipSurfaceColor) {
12901293
this.chipSurfaceColor = chipSurfaceColor;
1291-
if (chipSurfaceColor != null && chipBackgroundColor != null) {
1292-
setFillColor(compositeSurfaceBackgroundColor());
1294+
if (isShapeThemingEnabled) {
1295+
if (chipSurfaceColor != null && chipBackgroundColor != null) {
1296+
setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor));
1297+
}
12931298
}
12941299
onStateChange(getState());
12951300
}
@@ -1350,30 +1355,21 @@ public void setChipBackgroundColor(@Nullable ColorStateList chipBackgroundColor)
13501355
this.chipBackgroundColor = chipBackgroundColor;
13511356
if (isShapeThemingEnabled) {
13521357
if (chipSurfaceColor != null && chipBackgroundColor != null) {
1353-
setFillColor(compositeSurfaceBackgroundColor());
1358+
setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor));
13541359
}
13551360
}
13561361
onStateChange(getState());
13571362
}
13581363
}
13591364

1360-
private ColorStateList compositeSurfaceBackgroundColor() {
1361-
if (chipSurfaceColor == null || chipBackgroundColor == null) {
1362-
return null;
1365+
private ColorStateList compositeSurfaceBackgroundColor(
1366+
@NonNull ColorStateList backgroundColor, @NonNull ColorStateList surfaceColor) {
1367+
int[] colors = new int[states.length];
1368+
for (int i = 0; i < states.length; i++) {
1369+
colors[i] = MaterialColors.layer(
1370+
surfaceColor.getColorForState(states[i], currentChipSurfaceColor),
1371+
backgroundColor.getColorForState(states[i], currentChipBackgroundColor));
13631372
}
1364-
int[] colors =
1365-
new int[] {
1366-
MaterialColors.layer(
1367-
chipSurfaceColor.getColorForState(states[0], currentChipSurfaceColor),
1368-
chipBackgroundColor.getColorForState(states[0], currentChipBackgroundColor)),
1369-
MaterialColors.layer(
1370-
chipSurfaceColor.getColorForState(states[1], currentChipSurfaceColor),
1371-
chipBackgroundColor.getColorForState(states[1], currentChipBackgroundColor)),
1372-
MaterialColors.layer(
1373-
chipSurfaceColor.getColorForState(states[2], currentChipSurfaceColor),
1374-
chipBackgroundColor.getColorForState(states[2], currentChipBackgroundColor)),
1375-
};
1376-
13771373
return new ColorStateList(states, colors);
13781374
}
13791375

lib/java/com/google/android/material/chip/res/color/mtrl_chip_background_color.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<selector xmlns:android="http://schemas.android.com/apk/res/android">
1919
<!-- 8% + 8% opacity -->
2020
<item android:alpha="0.18" android:color="?attr/colorOnSurface" android:state_enabled="true" android:state_selected="true"/>
21+
<item android:alpha="0.18" android:color="?attr/colorOnSurface" android:state_enabled="true" android:state_checked="true"/>
2122
<!-- 12% of 87% opacity -->
2223
<item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
2324
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>

0 commit comments

Comments
 (0)