Elijah M
Elijah M

Reputation: 845

(Android) How to set colors.xml data using Java programatically?

I want to modify the value of a color in colors.xml, such as

<color name="colorPrimary">#3D3D3D</color>. I want to change this using Java programatically so I can modify this while the app is running.

I have tried things like R.color.colorPrimary = "#FFFFFF"; but this throws an incompatable types error because R.color.primary must be set to a int, not a String.

How do I change the colors.xml data using Java?

Upvotes: 0

Views: 78

Answers (2)

Ezaldeen sahb
Ezaldeen sahb

Reputation: 739

you can't change these values while the app is running, instead, make two colors in colors.xml and then you can access them using JAVA getResources().getColor(R.id.colorPrimary) or if you want to change the theme colors you can make different themes with different colors and change between them in runtime, more info in here

Upvotes: 1

Alejandro Gonzalez
Alejandro Gonzalez

Reputation: 1371

If you want to change the color a component, you only need to put the next code

bbbb.setTextColor(ContextCompat.getColor(context!!, int codecolor))
ccc.setBackgroundColor(ContextCompat.getColor(context!!, int codeColor))

Upvotes: 0

Related Questions