'Allow users to choose their own colours in Android app

I'm trying to allow users to create their own themes by using a colour picker within my app. I've learned that I essentially have two options here;

  1. Create pre-defined xml themes/styles and allow the user to choose which one to use.
  2. Allow the user to create their own colours, and save the values in SharedPreferences. Then apply these colours programatically.

Option 2 is what I am trying to do, as I want the user to have the ability to choose their own colours.

I am able to save the users choice to SharedPreferences, but applying the colours throughout the app is where I am stuck. For one, going through each button, text field, etc, and changing the colours will result in a long messy function which I'd like to avoid. But also, my buttons have drawables as their backgrounds, and I cannot figure out how to change the background colour of the drawable.

I have tried this, but it has no effect (I have hard coded the colour value for now just for testing).

  ColorFilter backgroundFilter = new BlendModeColorFilter(getResources().getColor(R.color.blue), BlendMode.DST);
  View button = findViewById(R.id.allApps);
  button.getBackground().setColorFilter(backgroundFilter);

So to summarize, the two things I need are;

  1. Ideally a better way to change the colours accross the whole screen without hard coding each button
  2. A way to change the colour of the background drawable of buttons


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source