Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize the look of checkbox starStyle

I the following inside the xml layout for my ListView row:

 <CheckBox
        android:id="@+id/favbutton"
        style="?android:attr/starStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

And it shows a blue star which can be checked to select a favourite. This is exactly the functionality that I need, but I just would like to customize the star icon that is shown. I would like to change the colour to yellow instead of blue and also make the star smaller if possible. Is it possible to make these changes by making changes to the theme applied to the application? What would I need to edit to change the look of the star icon?

like image 466
BruceHill Avatar asked Feb 25 '13 00:02

BruceHill


1 Answers

I resolved this as follows. I created a file in res/drawable called btn_star_selector.xml and defined the selector as follows:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="true" android:state_pressed="true"  android:drawable="@drawable/btn_star_on_pressed" />
     <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/btn_star_off_pressed" />
     <item android:state_checked="true" android:drawable="@drawable/btn_star_on" />
     <item android:state_checked="false" android:drawable="@drawable/btn_star_off" />
</selector>

With this selector defined I then replaced this line in the checkbox definition

style="?android:attr/starStyle"

with this

android:button="@drawable/btn_star_selector"

I then created four images to represent the different states of the checkbox, namely on, off, on pressed and off pressed and also placed these in res/drawables.

Hope this helps anyone else that is trying to figure out how to customize checkboxes.

like image 168
BruceHill Avatar answered Oct 20 '22 23:10

BruceHill