In my app I've ColorPicker
that is AmbilWarna color picker
. When I select the desired color from ColorPicker
, it works like charm. But when I close and restart the Activity, the color changes to default. Hence I'm trying to use SharedPreferences
to make it work, I've tried SharedPreferences
in EVERY way to make this succeed but it's not working! Trying to solve this problem from weeks, but no solution! Any help would be gladly appreciated! Here's my code:
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
}
public void colorpicker() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 0xff0000ff,
new OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
MainActivity.this.findViewById(R.id.activity_main)
.setBackgroundColor(color);
}
});
dialog.show();
}
}
public class MainActivity extends Activity {
SharedPrefrence colors;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
colors=getSharedPrefrences("MyColor",1);
int colorcode=shared.getInt("color", 0);
if(colorcode!=0)
{
MainActivity.this.findViewById(R.id.activity_main)
.setBackgroundColor(colorcode);
}
}
public void colorpicker() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 0xff0000ff,
new OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
MainActivity.this.findViewById(R.id.activity_main)
.setBackgroundColor(color);
edit=colors.edit();
edit.putInt("color",color);
edit.commit();
}
});
dialog.show();
}
}