Skip to content

Commit

Permalink
[Card] Fix crash on Pre-Q devices when checked icon is null
Browse files Browse the repository at this point in the history
LayerDrawable has this bug that if any of the provided drawables are null during creation, it crashes. The fix is only available on Q+. Fixes this by providing a dummy drawable when we find checked icon is null.

Resolves #2532

PiperOrigin-RevId: 426179927
  • Loading branch information
drchen authored and pekingme committed Feb 4, 2022
1 parent 810541e commit b4cab87
Showing 1 changed file with 7 additions and 1 deletion.
Expand Up @@ -25,6 +25,7 @@
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
Expand Down Expand Up @@ -88,6 +89,10 @@ class MaterialCardViewHelper {

private static final int CHECKED_ICON_LAYER_INDEX = 2;

// We need to create a dummy drawable to avoid LayerDrawable crashes on API 28-.
private static final Drawable CHECKED_ICON_NONE =
VERSION.SDK_INT <= VERSION_CODES.P ? new ColorDrawable() : null;

@NonNull private final MaterialCardView materialCardView;
@NonNull private final Rect userContentPadding = new Rect();

Expand Down Expand Up @@ -387,11 +392,12 @@ Drawable getCheckedIcon() {
}

void setCheckedIcon(@Nullable Drawable checkedIcon) {
this.checkedIcon = checkedIcon;
if (checkedIcon != null) {
this.checkedIcon = DrawableCompat.wrap(checkedIcon).mutate();
DrawableCompat.setTintList(this.checkedIcon, checkedIconTint);
setChecked(materialCardView.isChecked());
} else {
this.checkedIcon = CHECKED_ICON_NONE;
}

if (clickableForegroundDrawable != null) {
Expand Down

0 comments on commit b4cab87

Please sign in to comment.