-2

I want to allow the user to set a primary and a secondary colour in the app. What is the best way to do this. From what I read, I can define different themes and switch between them like

setTheme(myTheme.xml);

I would rather not have to do this at the start of every activity. Is there a way to somehow do this in the xml itself or what would be a best practice approach here?

Ideally I am looking for a 'global' approach, something that would work like making MyCurrentTheme in xml pointing to a variable that switches between theme1, theme_2, .... based on a user setting

<activity android:theme="@style/MyCurrentTheme">
chrise
  • 4,039
  • 3
  • 39
  • 74
  • What do you mean by "primary and secondary colour". Theme is set of colors and styles, while primary and secondary colors are used to customize android views. Primary and secondary colors are just subset of theme – Jakoss Jul 08 '16 at 11:47
  • Possible duplicate of [How to change current Theme at runtime in Android](http://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android) – Janki Gadhiya Jul 08 '16 at 11:50
  • The solutions there are using setTheme. I would like to avoid this and somehow just use xmls – chrise Jul 08 '16 at 12:00

2 Answers2

0

You can create different themes with required color schemes. You can then apply them through the manifest by defining theme in activity tag.

<activity android:theme="@style/CustomTheme">
  • yea, what I was looking for would be a way to make CustomTheme here somehow variable, so that I only need to control that variable isntead of every view/activity – chrise Jul 08 '16 at 12:03
  • For changing all elements of the theme, defining separate themes would be more suitable. If you want to change just colors in some elements, you can do them individually. What are you trying to achieve actually? – Ayush Maharjan Jul 08 '16 at 12:08
  • I want the user to be able to switch for example from one colour scheme for the app to another, so 'from a blue app to a green app'. that's all. but it seems that there should be a way to just globally switch between themes instead of coding it into every view and activity – chrise Jul 08 '16 at 12:11
  • You want to change the theme for the entire app? – Ayush Maharjan Jul 08 '16 at 12:12
  • I guess there isn't a way to switch theme that easily. You need to set the theme programatically. – Ayush Maharjan Jul 08 '16 at 12:24
0

You can create multiple theme for single app like below.

Tutorial here

Note: you have to call setTheme() before setContentView() methods.

enter image description here enter image description here

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41