Skip to content

Commit

Permalink
[Checkbox] Fixed pre 21 issue where the icon did not update its color…
Browse files Browse the repository at this point in the history
… according to its state properly

PiperOrigin-RevId: 466974893
(cherry picked from commit ccf67f7)
  • Loading branch information
leticiarossi committed Aug 11, 2022
1 parent a394314 commit 2cc8932
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions lib/java/com/google/android/material/checkbox/MaterialCheckBox.java
Expand Up @@ -276,15 +276,9 @@ && isButtonDrawableLegacy(attributes)) {

refreshButtonDrawable();

// This is needed due to a KitKat bug where the drawable states don't get updated correctly
// in time.
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
post(
() -> {
if (buttonIconDrawable != null) {
buttonIconDrawable.invalidateSelf();
}
});
// This is needed due to a pre-21 bug where the drawable states don't get updated correctly.
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP && buttonIconDrawable != null) {
post(() -> buttonIconDrawable.jumpToCurrentState());
}
}

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

currentStateChecked = DrawableUtils.getCheckedState(drawableStates);

if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
jumpDrawablesToCurrentState();
}
updateIconTintIfNeeded();

return drawableStates;
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
updateIconTintIfNeeded();
}

@Override
public void setChecked(boolean checked) {
setCheckedState(checked ? STATE_CHECKED : STATE_UNCHECKED);
Expand Down Expand Up @@ -468,7 +466,6 @@ public void setErrorShown(boolean errorShown) {
}
this.errorShown = errorShown;
refreshDrawableState();
jumpDrawablesToCurrentState();
for (OnErrorChangedListener listener : onErrorChangedListeners) {
listener.onErrorChanged(this, this.errorShown);
}
Expand Down Expand Up @@ -735,14 +732,6 @@ private void refreshButtonDrawable() {
refreshDrawableState();
}

@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (buttonIconDrawable != null) {
buttonIconDrawable.jumpToCurrentState();
}
}

/**
* Set the transition animation from checked to unchecked programmatically so that we can control
* the color change between states.
Expand Down Expand Up @@ -782,6 +771,19 @@ private void updateButtonTints() {
}
}

/*
* Update the icon tint due to a pre-21 bug where the drawable states don't get updated correctly.
*/
private void updateIconTintIfNeeded() {
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP
&& buttonIconDrawable != null
&& buttonIconTintList != null) {
buttonIconDrawable.setColorFilter(
DrawableUtils.updateTintFilter(
buttonIconDrawable, buttonIconTintList, buttonIconTintMode));
}
}

@RequiresApi(VERSION_CODES.R)
@Override
public void setStateDescription(@Nullable CharSequence stateDescription) {
Expand Down

0 comments on commit 2cc8932

Please sign in to comment.