Last active
August 29, 2015 14:00
-
-
Save evant/11379362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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