Augier
Augier

Reputation: 364

Apply a particular theme to a drawable

As stated in the title, I'd like to be able to programmatically apply a theme to a drawable resource.

I have an XML drawable resource which reference colors using ?attr/logoColorPrimary. If I define the colors in the main them, it works correctly and the drawable is displayed with the correct color.

Now I'd like to use that same drawable resource elsewhere in my application, but using different colors. Is there a way I can programmatically apply a different theme to that drawable? Of course, I tried to use the style XML attribute on the AppCompatImageView that drawable. But, as it turns out, locally redifining the styl on the view does not override the attributes that are passed down to the drawable.

Upvotes: 0

Views: 411

Answers (1)

Jaime
Jaime

Reputation: 388

Try using getDrawable there is a param for the theme, something like

val yourTheme = ContextThemeWrapper(baseContext, R.style.AppTheme).theme
ResourcesCompat.getDrawable(resources, R.drawable.your_drawable, yourTheme)

Upvotes: 1

Related Questions