Skip to content

Commit ccf67f7

Browse files
committed
[Checkbox] Fixed pre 21 issue where the icon did not update its color according to its state properly
PiperOrigin-RevId: 466974893
1 parent 7f98ebc commit ccf67f7

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

lib/java/com/google/android/material/checkbox/MaterialCheckBox.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,9 @@ && isButtonDrawableLegacy(attributes)) {
276276

277277
refreshButtonDrawable();
278278

279-
// This is needed due to a KitKat bug where the drawable states don't get updated correctly
280-
// in time.
281-
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
282-
post(
283-
() -> {
284-
if (buttonIconDrawable != null) {
285-
buttonIconDrawable.invalidateSelf();
286-
}
287-
});
279+
// This is needed due to a pre-21 bug where the drawable states don't get updated correctly.
280+
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP && buttonIconDrawable != null) {
281+
post(() -> buttonIconDrawable.jumpToCurrentState());
288282
}
289283
}
290284

@@ -338,13 +332,17 @@ protected int[] onCreateDrawableState(int extraSpace) {
338332

339333
currentStateChecked = DrawableUtils.getCheckedState(drawableStates);
340334

341-
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
342-
jumpDrawablesToCurrentState();
343-
}
335+
updateIconTintIfNeeded();
344336

345337
return drawableStates;
346338
}
347339

340+
@Override
341+
public void setEnabled(boolean enabled) {
342+
super.setEnabled(enabled);
343+
updateIconTintIfNeeded();
344+
}
345+
348346
@Override
349347
public void setChecked(boolean checked) {
350348
setCheckedState(checked ? STATE_CHECKED : STATE_UNCHECKED);
@@ -468,7 +466,6 @@ public void setErrorShown(boolean errorShown) {
468466
}
469467
this.errorShown = errorShown;
470468
refreshDrawableState();
471-
jumpDrawablesToCurrentState();
472469
for (OnErrorChangedListener listener : onErrorChangedListeners) {
473470
listener.onErrorChanged(this, this.errorShown);
474471
}
@@ -735,14 +732,6 @@ private void refreshButtonDrawable() {
735732
refreshDrawableState();
736733
}
737734

738-
@Override
739-
public void jumpDrawablesToCurrentState() {
740-
super.jumpDrawablesToCurrentState();
741-
if (buttonIconDrawable != null) {
742-
buttonIconDrawable.jumpToCurrentState();
743-
}
744-
}
745-
746735
/**
747736
* Set the transition animation from checked to unchecked programmatically so that we can control
748737
* the color change between states.
@@ -782,6 +771,19 @@ private void updateButtonTints() {
782771
}
783772
}
784773

774+
/*
775+
* Update the icon tint due to a pre-21 bug where the drawable states don't get updated correctly.
776+
*/
777+
private void updateIconTintIfNeeded() {
778+
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP
779+
&& buttonIconDrawable != null
780+
&& buttonIconTintList != null) {
781+
buttonIconDrawable.setColorFilter(
782+
DrawableUtils.updateTintFilter(
783+
buttonIconDrawable, buttonIconTintList, buttonIconTintMode));
784+
}
785+
}
786+
785787
@RequiresApi(VERSION_CODES.R)
786788
@Override
787789
public void setStateDescription(@Nullable CharSequence stateDescription) {

0 commit comments

Comments
 (0)