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)));
}
});
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)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.