Center CheckBox drawable within itself
I believe the problem is that the Checkbox widget uses a regular TextView with the drawableLeft
attribute, because it expects text to be shown as well. (This is why you see it centered vertically, but offset slightly to the left.)
If you simply want an image button with multiple states, I suggest using a ToggleButton with your custom images in a state list selector. Or you could create a custom class that extends ImageView and implements Checkable.
You can use a parent layout to achieve this :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<CheckBox
android:id="@+id/checkbox_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Check box text" />
</LinearLayout>
I'm using MaterialCheckBox
from material-components-android
(or AppCompatCheckBox
. They're similar).
I find that if a CheckBox doesn't have any text, setting its android:minWidth="0dp"
and android:minHeight="0dp"
can remove all paddings and center the drawable.