In general, there is a program made in a single color manner, all colors are described and loaded from a color resource. But now I want to create another version of the colors and allow the user to choose it. Of course, the simplest thing that comes to mind is simply to create your own resource-class, in which, by condition, you choose a color. But it will have to be rewritten a lot to adapt the program to this. Maybe there is a good way, for example, to programmatically change the color resource file or different versions of a file or theme ...?
- So everything seems to be clear. It is necessary to make it possible for the user to change the colors of the program in the settings. At the same time, the colors themselves are stored in color resources. How to do it? - Uraty
- the easiest way through if - to substitute the desired topic, for more difficult - there were articles on Habré - Gorets
|
2 answers
Unfortunately, refactoring is indispensable.
- We make a class an interlayer between resources and display
- Save the value of the selected theme (for example, in
Preference) .setColor(MyColors.createNested(color))colors programmatically (.setColor(MyColors.createNested(color)))- Inside
Color createNested(Color color)having the color parameters and the saved value - we return the required color - Profit
|
If you need ready-made changeable themes, then you need to set attributes for all changeable resources in the attr.xml file, then create a topic with a specific name and define the value of each attribute in it. After in the base Activity class in onCreate, set the desired setTheme (id) theme. Well, in the layout files, respectively, you refer not to specific resources, but to attributes.
Or, if you just need the user to change specific colors, then for each color you need to create an entry in SharedPreferences, and substitute the necessary values when initializing the interface.
|