user740006
user740006

Reputation: 1847

Customizing Android checkbox

I am trying to partially change the images for Android's checkboxes. Following the tutorial here, I have done the following experiment:

<CheckBox
  android:checked="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<CheckBox
  android:checked="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:button="@android:drawable/btn_check_on"
  android:background="@android:drawable/btn_check_label_background"/>    
<CheckBox
  android:checked="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:button="@drawable/cb_on"
  android:background="@drawable/cb_background"/>

For the third checkbox, I have copied the images btn_check_on.png and btn_check_label_background.9.png from Android's SDK to the project's res/drawable and rename them to cb_on.png and cb_background.png. While I expected the three checkboxes to have identical appearances, surprisingly, the third checkbox is larger than the first two. Can anybody explain why? How can I fix this problem?

Upvotes: 1

Views: 2638

Answers (3)

Jokahero
Jokahero

Reputation: 1074

Try renaming your image cb_background.png in cb_background.9.png

.9 means that the image is a nine-patch image which is done to rescale itself automatically.

I think that why the third image is larger.

For more information about nine-patch: here

Upvotes: 2

Alec B. Plumb
Alec B. Plumb

Reputation: 2490

@Jokahero is right about the 9-patch, and @jkhouw1 is right about the resolutions. For checkboxes, you will also need to know about StateListDrawable.

Upvotes: 1

jkhouw1
jkhouw1

Reputation: 7350

My guess would be that you only copied one resolution of the drawables. You need to copy all of them (mdpi, hdpi,ldpi) to the appropriate res folder (drawable-mdpi, drawable-hdpi, drawable-ldpi)

Upvotes: 1

Related Questions