Search code examples
javaandroidmaterial-designandroid-theme

How to get boolean from custom boolean attribute for current theme?


I am setting custom attribute with Boolean format in my theme.

<attr name="isCompound" format="boolean" />

When I am trying to retrieve this attribute in my code using following

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.isCompound, typedValue, true);
if(typedValue.data) {
    ...
}

I get error that typedValue.data (int) can not be converted to Boolean. My question is how do I get Boolean value isCompound from the current theme?


Solution

  • Thanks to @Mike M. for the comment

    Blockquote "The data field holds 0 or 1 that was originally specified as "false" or "true"." – https://developer.android.com/reference/android/util/TypedValue#TYPE_INT_BOOLEAN

    Document says that it returns 1 for true and 0 for false. But when I test it returns -1 for true and 0 for false.

    I guess, safe way would be to check for false.