Skip to content Skip to sidebar Skip to footer

Pick A Color From The Holocolorpicker Then Saved Using Shared Preferences

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 Setti

Solution 1:

This is how sharedpreference used:

// put intSharedPreferencessharedpreferences= getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
Editoreditor= sharedpreferences.edit();
editor.putInt("back_colorcode", backcolorValue);
editor.commit();

// get intSharedPreferencessharedpreferences= getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
intbackcolorValue= sharedpreferences.getInt("back_colorcode", 0)

Solution 2:

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.

Solution 3:

onColorChanged method parameter is color code selected from ColorPicker. save same in SharedPreferences :

publicvoidonColorChanged(int color) {
    Editoreditor0= 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);
Viewview=this.getWindow().getDecorView();
view.setBackgroundColor(colorCode);

Post a Comment for "Pick A Color From The Holocolorpicker Then Saved Using Shared Preferences"