android – Why custom attributes work for tablayout while it doesn't work for checkbox-ThrowExceptions

Exception or error:

Let me get straight to the point. I have several themes and not fan of writing large amount of codes, specially in XML files that’s why I am using this aproach, Also I am targeting API >=21.

In my tablayout, I have three tabs with custom drawables. By default all of them are grey, and when user select one of them, its color change to something else. Each theme has its unique color for this matter. So I have:

values/attrs.xml

<declare-styleable name="TableLayoutColor">
    <attr name="selectedTab" format="color" />
    <attr name="unselectedTab" format="color"/>
</declare-styleable>

color/white_tab_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/selectedTab"
        android:state_selected="true" />
    <item android:color="?attr/unselectedTab" />
</selector>

And in my styles, one of my themes is the folowing:

values/styles.xml

<style name="MainWhite" parent="MotherWhite">
    <item name="selectedTab">@color/materialBlue</item>
    <item name="unselectedTab">@color/darkGrey</item>
</style>

It works perfectly fine, I use it like:

   <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:saveEnabled="true"
        app:tabIndicator="@null"
        app:tabIconTint="@color/white_tab_color">

Easy. Every Time I decide to add a new theme, I just change styles.xml.

Now My problem is checkbox with custom attribute. I want to make it so that each theme have its unique checkbox, Surely there are other ways, but I just want to change style.xml every time.
With order above

attr.xml

<declare-styleable name="MainCheckBoxRadioButton">
    <attr name="checkBoxColorActive" format="color" />
    <attr name="checkBoxColorNormal" format="color" />
</declare-styleable>

white_checkbox_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/checkBoxColorActive"
        android:state_checked="true" />
    <item android:color="?attr/checkBoxColorNormal"
        android:state_checked="false"  />
</selector>

and styles.xml

<style name="MainWhite" parent="MotherWhite">
    <item name="checkBoxColorNormal">@color/something</item>
    <item name="checkBoxColorActive">@color/somethingelse</item>
</style>

I use it like

<CheckBox
    android:buttonTint="@color/white_checkbox_color"
    android:id="@+id/checkbox1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</CheckBox>

Doesn’t work. It just shows a red color that is not @color/something and @color/somethingelse. Where did I go wrong? I believe this question is related, but its answers doesn’t sound very good to me. In the case of tablayout, I have no problem. To make matter worse, my XML preview shows checkbox as it should be. But when I run it in simulator or something I get wrong results. Also if i replace ?attr/… in white_checkbox_color.xml with something direct, like @color/something, it will work but I don’t want that because I’d have to write white_checkbox_color.xml for each theme separately.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *