0

In my colors.xml file I have a value defined for example as:

<color name="red">#ffff0000</color>

How can I change the value to a different color programatically?

Jinbonka
  • 123
  • 1
  • 11
user2779837
  • 301
  • 3
  • 15

2 Answers2

0

Help from another thread:

One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.

This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time. This is the best solution what I have found.

Change value of R.string programically Thx to Andro Selva

Community
  • 1
  • 1
Adam Wigren
  • 383
  • 2
  • 11
-1

You could have both colors in color.xml and change it before display/activity focus.

<color name="red">#ffff0000</color>
<color name="blue">#0000FF</color>

With canvas:

Paint paint = new Paint();
if(shouldBeRed()) {
  paint.setColor(R.color.red);
} else {
  paint.setColor(R.color.blue);
}
canvas.drawRect(rect, paint);

@user2779837 where do you want to change the color?

Jan-Terje Sørensen
  • 14,468
  • 8
  • 37
  • 37