Skip to content

Commit

Permalink
[Card] Fix checked icon gravity issues
Browse files Browse the repository at this point in the history
Re-measures the card when checked icon gravity changes in order to move the icon position. Also corrects the padding adjustment logic for pre-lolipop or when compat paddings are being used.

PiperOrigin-RevId: 408876717
  • Loading branch information
drchen committed Nov 10, 2021
1 parent 38406e1 commit 283715d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
Expand Up @@ -204,7 +204,7 @@ public void onInitializeAccessibilityEvent(@NonNull AccessibilityEvent accessibi
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
cardViewHelper.onMeasure(getMeasuredWidth(), getMeasuredHeight());
cardViewHelper.recalculateCheckedIconPosition(getMeasuredWidth(), getMeasuredHeight());
}

/**
Expand Down
Expand Up @@ -418,30 +418,32 @@ void setCheckedIconMargin(@Dimension int checkedIconMargin) {
this.checkedIconMargin = checkedIconMargin;
}

void onMeasure(int measuredWidth, int measuredHeight) {
void recalculateCheckedIconPosition(int measuredWidth, int measuredHeight) {
if (clickableForegroundDrawable != null) {
boolean isPreLollipop = VERSION.SDK_INT < VERSION_CODES.LOLLIPOP;
int verticalPaddingAdjustment = 0;
int horizontalPaddingAdjustment = 0;
if (isPreLollipop || materialCardView.getUseCompatPadding()) {
verticalPaddingAdjustment = (int) Math.ceil(2f * calculateVerticalBackgroundPadding());
horizontalPaddingAdjustment = (int) Math.ceil(2f * calculateHorizontalBackgroundPadding());
}

int left =
isCheckedIconEnd()
? measuredWidth - checkedIconMargin - checkedIconSize
? measuredWidth - checkedIconMargin - checkedIconSize - horizontalPaddingAdjustment
: checkedIconMargin;
int bottom =
isCheckedIconBottom()
? checkedIconMargin
: measuredHeight - checkedIconMargin - checkedIconSize;

boolean isPreLollipop = VERSION.SDK_INT < VERSION_CODES.LOLLIPOP;
if (isPreLollipop || materialCardView.getUseCompatPadding()) {
bottom -= (int) Math.ceil(2f * calculateVerticalBackgroundPadding());
left -= (int) Math.ceil(2f * calculateHorizontalBackgroundPadding());
}
: measuredHeight - checkedIconMargin - checkedIconSize - verticalPaddingAdjustment;

int right =
isCheckedIconEnd()
? checkedIconMargin
: measuredWidth - checkedIconMargin - checkedIconSize;
: measuredWidth - checkedIconMargin - checkedIconSize - horizontalPaddingAdjustment;
int top =
isCheckedIconBottom()
? measuredHeight - checkedIconMargin - checkedIconSize
? measuredHeight - checkedIconMargin - checkedIconSize - verticalPaddingAdjustment
: checkedIconMargin;

if (ViewCompat.getLayoutDirection(materialCardView) == ViewCompat.LAYOUT_DIRECTION_RTL) {
Expand All @@ -451,8 +453,7 @@ void onMeasure(int measuredWidth, int measuredHeight) {
left = tmp;
}

clickableForegroundDrawable.setLayerInset(
CHECKED_ICON_LAYER_INDEX, left, top /* top */, right, bottom);
clickableForegroundDrawable.setLayerInset(CHECKED_ICON_LAYER_INDEX, left, top, right, bottom);
}
}

Expand Down Expand Up @@ -686,6 +687,8 @@ int getCheckedIconGravity() {

void setCheckedIconGravity(@CheckedIconGravity int checkedIconGravity) {
this.checkedIconGravity = checkedIconGravity;
recalculateCheckedIconPosition(
materialCardView.getMeasuredWidth(), materialCardView.getMeasuredHeight());
}

private boolean isCheckedIconEnd() {
Expand Down

0 comments on commit 283715d

Please sign in to comment.