customize check box preference
Asked Answered
M

2

15

I am unable to customize my checkbox , although I have defined the background in the xml preference file, it doesn't pull the file. 1. I am trying to display custom images for checkbox and have defined the selector xml as "android_button.xml" which looks like :

   <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checkable="true"
      android:drawable="@drawable/state_normal" /> <!-- pressed -->
<item  android:state_checked="true"
      android:drawable="@drawable/android_pressed" /> <!-- focused -->
<item android:drawable="@drawable/state_normal" /> <!-- default -->
</selector>

state_normal and android_pressed are two .png images in res>drawable folder.

2.my Checkbox preference.xml file is :

          <CheckBoxPreference android:key="@string/Drop_Option"
            android:title="Close after call drop"
            android:defaultValue="true"
            android:background="@drawable/android_button"
            />

Is there any error in the definition, The only change that shows up in screen is the android:title text, if I change the text, it changes the text. Nothing else changes. How do I fix this. Thank you for your suggestion.

Myeloid answered 25/8, 2010 at 19:15 Comment(0)
M
44

There are two ways to achieve what you need, first is to define custom checkbox layout custom_chexbox.xml at res/layout:

<?xml version="1.0" encoding="UTF-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/checkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:clickable="false" android:button="@drawable/android_button"/>

Then you need to specify this layout for the preference:

<CheckBoxPreference android:key="@string/Drop_Option"
 android:title="Close after call drop" android:defaultValue="true"
 android:widgetLayout="@layout/custom_checkbox"/>

Second way is to create a custom theme, redefine style for checkbox views and apply the theme to the preferences activity, see How to customize the color of the CheckMark color in android in a dialog. : android for details.

Malpractice answered 26/8, 2010 at 2:36 Comment(6)
sick level, had no clue about android:widgetLayout, wish every other component had that.Professor
This works perfectly man. You are a life-saver. I never knew that we can use custom layout for widgets too. Thanks a lot.Cyclamen
Works customize the checkbox, but the checkbox doesn't reflect the check of the preference :(Polygynous
Ops, my bad. I use a different ID name, apparently. Up one vote for this superb solution!!Polygynous
Also, do set android:background="@null", to make the checkbox click truly transparent (from the ripple effect of lollypop especially)Polygynous
One thing I didn't pay close attention to at first was the ID. It's very important to have the ID be set as shown in the answer here, otherwise you won't see the field update when you click anywhere else in the element!Pulpiteer
M
0

Make one drwable xml file :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true"></item>
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false" android:state_focused="true"></item>
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false"></item>
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_focused="true"></item>
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_pressed="true"></item>
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false"></item>
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false" android:state_focused="true"></item>
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false"></item>
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_focused="true"></item>
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_pressed="true"></item>

</selector>

Set it programatically by cb.setButtonDrawable(R.drawable.checkboxcustom);

Monstrous answered 30/6, 2016 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.