Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolveAttribute always returns null for attributes

My theme:

<style name="AppThemeDark" parent="Theme.AppCompat">
    <item name="AppButton">@drawable/standard_dark_button_selector</item>
</style>    

This code i use to get a themed drawable:

public static Drawable getThemedDrawable(Context context, int resource) {
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(resource, typedValue, true);
    if(typedValue.resourceId == 0) {
        return context.getResources().getDrawable(R.drawable.standard_theme_not_found);
    }
    else
        return context.getResources().getDrawable(typedValue.resourceId);
}

And this is the direct call:

positive.setBackgroundDrawable(Theme.getThemedDrawable(getBaseContext(), R.attr.AppButton));

The typedValue.resourceId is always 0 and returns no real drawable resource id, in this case it always returns R.drawable.standard_theme_not_found.

When i use this code with colors (coming from typedValue.data) its filled and does work.

How can i solve this?

like image 936
Fabian Avatar asked Sep 26 '26 10:09

Fabian


1 Answers

i found the answer myself, its important, that the theme is set properly in the context.

if you apply a theme programmatically:

setTheme(myTheme);

then the theme is not saved in the getBaseContext()

so you need to use

Theme.getThemedDrawable(myActivity, R.attr.AppContainer1Background)

instead of

Theme.getThemedDrawable(getBaseContext(), R.attr.AppContainer1Background)
like image 115
Fabian Avatar answered Sep 29 '26 00:09

Fabian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!