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:


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


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

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


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

It works perfectly fine, I use it like:


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


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


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

and styles.xml

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

I use it like


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 *