Reputation: 1005
am trying to save he color picked from the holocolorpicker and use it in another activity
before the onCreate method in the settings activity i put this lines
private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;
after the onCreate method in the settings activity i put this lines
public void onColorChanged(int color) {
ColorPicker picker0 = (ColorPicker) findViewById(R.id.backpicker);
backcolorValue = picker0.getColor();
Editor editor0 = backcolorprefs.edit();
editor0.clear();
editor0.putInt("back_colorcode", backcolorValue);
editor0.commit();
}
before the onCreate method of the other activity i put this lines
private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;
in the onCreate method of the other activity i put this lines
backcolorprefs = getSharedPreferences(SettingsTAG0, 0);
backcolorprefs.getInt("back_colorcode", backcolorValue);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(backcolorValue);
i am a super newbie to android and java but i make a try but nothing is happened
any help please
Upvotes: 0
Views: 559
Reputation: 132992
onColorChanged
method parameter is color code selected from ColorPicker. save same in SharedPreferences :
public void onColorChanged(int color) {
Editor editor0 = backcolorprefs.edit();
editor0.clear();
editor0.putInt("back_colorcode", color);
editor0.commit();
}
In second Activity get color code from preference and pass to setBackgroundColor
:
int colorCode=backcolorprefs.getInt("back_colorcode", Color.BLACK);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colorCode);
Upvotes: 0
Reputation: 6138
This is how sharedpreference used:
// put int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("back_colorcode", backcolorValue);
editor.commit();
// get int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
int backcolorValue = sharedpreferences.getInt("back_colorcode", 0)
Upvotes: 1
Reputation: 8598
Nothing happened because you are not assigning value read from preferences to backcolorValue. This line:
backcolorprefs.getInt("back_colorcode", backcolorValue);
simply reads it and DOESN'T store in backcolorValue. Do:
backcolorValue = backcolorprefs.getInt("back_colorcode", backcolorValue);
Also, in onColorChanged, I don't see that you're initialising backcolorprefs, so make sure you do. Btw, for getSharedPreferences, you should use constant Context.MODE_PRIVATE, instead of hardcoded value of 0.
Upvotes: 0