Reputation: 23
I want to save the color by clicking the button and set this color on others activities.
I use code to change it but this code doesn't save the color. What should I add in my code?
mYellowColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportActionBar().setBackgroundDrawable(new
ColorDrawable(getResources().getColor(R.color.yello)));
}
});
Upvotes: 0
Views: 33
Reputation: 174
Define Shared Preferences in your class
private SharedPreferences sharedPreferences;
Inside onCreate add
sharedPreferences = getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
And then inside onClickListener
mYellowColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String hexColor = "ADD YOUR HEX CODE HERE";
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(hexColor)));
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("toolbarColor",hexColor);
editor.commit();
}
});
On next activity again define Shared Preferences and add following code to onCreate
sharedPreferences = getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
String hexColor = sharedPreferences.getString("toolbarColor", "");
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(hexColor)));
Upvotes: 1