Skip to content

Instantly share code, notes, and snippets.

@evant
Last active August 29, 2015 14:00
Show Gist options
  • Save evant/11379362 to your computer and use it in GitHub Desktop.
Save evant/11379362 to your computer and use it in GitHub Desktop.
/**
* Correctly sets the left padding between the drawable and text in a CheckBox. This is
* necessary because the behavior of setPadding() changed in 4.2
*
* @param checkBox the checkbox to pad
* @param buttonWidth the width in pixels of the button drawable used in the CheckBox
* @param paddingLeft the padding in pixels between the drawable and the text
*/
@SuppressLint("NewApi")
public static void setCheckboxPaddingLeft(CheckBox checkBox, int buttonWidth, int paddingLeft) {
// Before 4.2, the padding ignored the width of the drawable
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
paddingLeft += buttonWidth;
}
checkBox.setPadding(
paddingLeft,
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom()
);
}
/** Usage **/
int leftPadding = getResources().getDimensionPixelSize(R.dimen.checkbox_padding_left);
int buttonWidth = getResources().getDrawable(R.drawable.custom_checkbox_button_drawable).getIntrinsicWidth();
Compat.setCheckboxPaddingLeft(mCheckbox, buttonWidth, leftPadding);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment