dimanche 27 novembre 2016

Check Box Style based on Theme

I have 2 themes, dark and light which will be applied on the of their respective buttons. For checkbox i have made 2 styles based on those two themes.

in XML

<CheckBox
    android:text="CheckBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox2"
    android:theme="?attr/check_style"/>

in attr.xml

   <attr name="check_style" format="reference"/>

in themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources  xmlns:android="http://ift.tt/nIICcg">
    <style name="dark" parent="AppTheme">
       <item name="check_style">@style/MyCheckBox1</item>

    </style>
    <style name="light" parent="AppTheme">
       <item name="check_style">@style/MyCheckBox3</item>
    </style>

</resources>

in styles.xml

<style name="MyCheckBox1" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="colorControlNormal">@android:color/black</item>
    <item name="colorControlActivated">@android:color/holo_orange_dark</item>
</style>
<style name="MyCheckBox3" parent="Widget.AppCompat.CompoundButton.CheckBox">
   <item name="android:drawable">@drawable/selector_checkbox</item>
</style>

in selector_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">
    <item android:drawable="@drawable/apptheme_btn_check_on_focused_holo_light" android:state_checked="true" />
    <item android:drawable="@drawable/apptheme_btn_check_off_focused_holo_light" android:state_checked="false" />
    <item android:drawable="@drawable/apptheme_btn_check_on_focused_holo_light" android:state_pressed="true" />
    <item android:drawable="@drawable/apptheme_btn_check_off_focused_holo_light"/>


</selector>

For the style for checkbox in Dark, it works like a charm, but for the checkbox in light theme, once checkbox gets selected , only checked drawable is showing, no matter how many times if i deselect the checkbox.

Please help how to resolve it. NOTE : if i use the selector in xml like

   android:button="@drawable/selector_checkbox"

it works perfect.




Aucun commentaire:

Enregistrer un commentaire