Funris
Funris

Reputation: 51

Change XML color programmatically

Is it possible to change XML colors via Kotlin/java code. There's only 5 colors in my app, and in the settings you can change the theme. I don't want to change each element to the new color, but I want to change the XML color so all the elements get the new colors. For example:

<color name="colorPrimary">#6c6c6c</color>

I want to change it whenever a switch element is ON to

<color name="colorPrimary">#212121</color>

The main Idea is I want all the elements to get their colors from a source.

Upvotes: 1

Views: 2480

Answers (1)

Lucas Cabrales
Lucas Cabrales

Reputation: 2153

It is not possible to change XML resource values at runtime as @CommonsWare commented.

In order to change all colors dinamically, the best way is to use app themes for that. Create a new theme and change it programmatically.

See: Themes

Upvotes: 2

Related Questions