1

I have implemented material design for my app, and I want the user to be able to customize the colors for colorPrimary, colorPrimaryDark, and colorAccent. How to do that?

I want to have a settings activity where the user can select their own color and the change will apply to all my activities. Thanks.

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Julia
  • 1,207
  • 4
  • 29
  • 47
  • Hey there, one guy asked similar question before and I post an answer maybe you will check it : [how-to-give-the-user-the-possibility-to-switch-between-different-colors-skin-in](http://stackoverflow.com/questions/38349187/how-to-give-the-user-the-possibility-to-switch-between-different-colors-skin-in/38350441) – Yasin Kaçmaz Aug 16 '16 at 15:39

1 Answers1

0

Depending on whether or not you are allowing the users to select from a static list of color or from a color picker you could save the colors they select in SharedPreferences and pull them back out when the app starts. Unfortunately this would mean you would have to set the colors of all the views you use those colors for via code rather than xml since themes are immutable. Check out this post: How to _really_ programmatically change primary and accent color in Android Lollipop? You could take the approach in the answer from devconsole; https://stackoverflow.com/a/37905131/3314615

Community
  • 1
  • 1
ClayHerendeen
  • 187
  • 1
  • 4
  • 15
  • yah, I want to offer them a color picker and stored their preferences. – Julia Aug 16 '16 at 17:18
  • To do this you will have to manage everything in code unfortunately. So you will have to set the background color and text color of everything in code to the color stored in sharedprefs. Sorry I wish there was a better way. For one app i've been working on that is a shell app for various clients i have a config.json file that i read in with all of the colors based on which client im build the app for. – ClayHerendeen Aug 16 '16 at 18:01
  • I would suggest writing subclasses for each type of View ( LinearLayout, EditText, TextView, etc. ) and setup the color in the constructor by grabbing it from sharedpreferences. Then use those custom classes in your layout files. This is a lot of work but more efficient and less code than programmatically changing the color of every view inside every fragment or activity. – ClayHerendeen Aug 16 '16 at 18:07