resolveAttribute returns -1 sometimes

178 Views Asked by At

I'm trying to get theme specific values programatically like so:

MainActivity.getInstance()?.theme?.resolveAttribute(R.attr.settingsTint, value, true)
value.data // sometimes its -1

the problem is that sometimes the values returned is -1 while the attribute does exists in attr.xml, as well is being used in the style.xml. also other attributes do return their correct resource id value.

anyonw knows what's the problem?

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Theme">
        .
        .
        .
        <attr name="settingsTint" format="color"/> 
        .
        .
        .
    </declare-styleable>

</resources>

style.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!--    Theme.AppCompat.Light.NoActionBar-->
    <style name="AppTheme" parent="Theme.MaterialComponents">
        <item name="settingsTint">@color/settingsTint</item>

    </style>

    <style name="AppTheme.Dark" parent="AppTheme">
        <item name="settingsTint">@color/settingsTint_dark</item>
    </style>

</resources>

manifest:

<application
        android:theme="@style/AppTheme"
<activity
            android:theme="@style/AppTheme">

at some point even before I change the theme I check the value MainActivity.getInstance()?.theme?.resolveAttribute(R.attr.settingsTint, value, true) as i said before and I see -1

thanks!

1

There are 1 best solutions below

0
On

Maybe you could check the TypedValue.type before you using it.

if (tv.type == TypedValue.TYPE_STRING){
    ...
}else if(tv.type == TypedValue.NULL){
    ...
}