Skip to content

Instantly share code, notes, and snippets.

@noaht11
Created September 1, 2016 05:21
Show Gist options
  • Save noaht11/0eb887ee1543a417421f4e4bd69b39b0 to your computer and use it in GitHub Desktop.
Save noaht11/0eb887ee1543a417421f4e4bd69b39b0 to your computer and use it in GitHub Desktop.
private void updatePadding() {
boolean chipsArePresent = !getAllChips().isEmpty();
if (!chipsArePresent && mUsingDefaultPadding) {
mUsingDefaultPadding = false;
Paint paint = getPaint();
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int textHeight = fm.descent - fm.ascent;
// Calculate how tall the view should be if there were chips
int newTextHeight = mChipHeight + (mChipVerticalSpacing != -1 ? mChipVerticalSpacing : 0);
// We need to add half our missing height above and below the text by increasing top and bottom padding
int paddingAdjustment = (newTextHeight - textHeight) / 2;
super.setPadding(getPaddingLeft(), mDefaultPaddingTop + paddingAdjustment, getPaddingRight(), mDefaultPaddingBottom + paddingAdjustment);
} else if (chipsArePresent && !mUsingDefaultPadding) {
// If there are chips we can revert to default padding
mUsingDefaultPadding = true;
super.setPadding(getPaddingLeft(), mDefaultPaddingTop, getPaddingRight(), mDefaultPaddingBottom);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment