I am making an app and was wondering if there was a way to change a color resource value programmatically. For example, I am using the resource R.color.text_color_name
to set the TextView
text color. Is there a way to set that value to something different so that it would change the color of every TextView
in the application?
Asked
Active
Viewed 7,591 times
2

Benjamin Owen
- 608
- 2
- 10
- 28
1 Answers
1
The R file contains constants, you cant change them at runtime because you can't normally change constants at run time. If you want to update the color of all textviews why dont you look into themes. Create a custom theme and then change the theme when activity loads in onCreate. After you set the new theme i think your going to have to call setContentView again and then call all your findViewByIds again as they will be null. You could also try Calling recreate() after setTheme(). This sounds messy.
Maybe this can help you change the theme.
-
Sorry for being an Android noob, but how can you change color values (default TextView, background color, toolbar color, etc.) from a style? I am setting some colors programmatically for backwards compatibility, would I have to change some internal variables as well? – Benjamin Owen Dec 17 '15 at 00:49
-
are you trying to change the colors from style file at runtime or at compile time ? and your talking styles not themes right ? – j2emanue Dec 17 '15 at 01:16
-
Runtime: I am talking about values in the `colors.xml` file. I was wondering if those values were unloaded to variables that I could change. – Benjamin Owen Dec 17 '15 at 01:17
-
No. You can't change those – j2emanue Dec 17 '15 at 03:37
-
So my best bet is to make themes with different colors and set the entire app theme. – Benjamin Owen Dec 17 '15 at 10:37