lokoko
lokoko

Reputation: 5803

How to set the Color in R.color programatically

I have a color resource defined in colors.xml. I want to set the value of this resource programatically. I have a settings option in my app where i would display a color palette to choose a color from and i want to set that selected color in the color resource.

Any suggestions ?

Upvotes: 1

Views: 1603

Answers (2)

Nathua
Nathua

Reputation: 8826

You can't edit your xml files like that, instead you should use local storage,

Save your color in sharedpreferences when they select it

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("color", your_color_id);
editor.commit();

Then read it when you start your activity or fragment etc

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int highScore = sharedPref.getInt("color", default_value);

Upvotes: 2

michal.luszczuk
michal.luszczuk

Reputation: 2903

You are not able to change resource values programatically, like colors.xml values. Instead of this you can save your 'settings' in SharedPreferences

Upvotes: 2

Related Questions