0

In my app I have to change all the colors runtime. The app is grey to begin with, and then asks the user to select their gender. When they select male then the app turns blue, and when they select female the app turns pink.

I have defined an AppTheme that styles all widgets, referring to colors in res/values/colors.xml and using res/drawable for backgrounds, which in turn also refer to the colors. All is seems pretty solid and neatly tied together, so I'd really like to reuse this!

I image that if I somehow replace the res/values/colors.xml, then everything would turn out fine, but I fear this is not possible and I don't know how to do this in the first place. I'm afraid I'd have to copy and paste my entire theme to AppTheme.Blue and AppTheme.Pink but that's quite horrible because that cascades into replacing close to every single drawable/*.

So how to do this? There has to be some trick where I can have separate colors.xml and switch between them...

Stephan Henningsen
  • 3,665
  • 1
  • 22
  • 29

1 Answers1

1

You can't change colors.xml, what you should do instead is create themes with theme attributes that point to colors. So in one theme the attribute primary_dark would point to black while in another them it can point to grey. Then have your styles point to the theme attributes rather than the colors directly. This way you can change your theme at runtime which will then change what colors are mapped to the attributes/styles. Here's a nice guide on how to do this http://www.hidroh.com/2015/02/25/support-multiple-themes-android-app-part-2/

FriendlyMikhail
  • 2,857
  • 23
  • 39