Custom CheckBox image gets cut off

android, checkbox

Solution

Today I had the same problem (my custom image was cutted on the left side). I fixed it putting:

android:button="@null"
android:background="@drawable/my_custom_checkbox_state.xml"

Problem

As you can see in the image above. There are three views on this screenshot. - The first item is `CheckBox` with text and having state off. - The second item is `CheckBox` without text and having state on. - The last item is `ImageView` with src pointing to the drawable image. The CheckBoxes were customized using `android:button`. As I tried using smaller images, all of the checkbox is left-aligned. Comparing these two images tell me that the default size of the `CheckBox` seems fixed to certain size until text attribute is large enough to require extending. There is nothing special in the file as well. See following. custom_cb.xml ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/creditcard_selected" android:state_checked="true" /> <item android:drawable="@drawable/creditcard"/> </selector> ``` layout.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/cbFalse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/custom_cb" android:text="" /> <CheckBox android:id="@+id/cbTrue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/custom_cb" android:focusable="false" android:checked="true" android:layout_toRightOf="@id/cbFalse" /> <ImageView android:id="@+id/imvTrue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/creditcard" android:layout_toRightOf="@id/cbTrue" /> </RelativeLayout> ``` Is there anyway I can use bigger image for CheckBox while keeping the size as `wrap_content`? If I set CheckBox `layout_width` to actual pixel or dp then it display full image but that mean I have to manually check for the size every time it change.

Original source