I realized that, even though the Sharedprefs, saves the checkbox state, it doesn’t keep the drawable resource background thingy that way…… any way to save that too? I was hoping it stays like the picture below. EDIT : So my objective would be that, On checkbox being checked, background changes, because of sharedprefs, the checked state is saved and on exiting the app, the checkbox remains checked but the background of the checkbox returns to its "un-highlighted" state without the drawable background
CheckBox C1,C2,C3;
//Creating keys for sharedpreference
private static final String C1key = "C1_key";
private static final String C2key = "C2_key";
private static final String C3key = "C3_key";
SharedPreferences shp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anime);
//sharedpreference created with the name as anime
shp = getSharedPreferences("Anime",MODE_PRIVATE);
//This is just background gradient animation
ConstraintLayout constraintLayout = findViewById(R.id.layout_anime);
AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setExitFadeDuration(4000);
animationDrawable.start();
//Initializing checkboxes
C1 = findViewById(R.id.C1);
C2 = findViewById(R.id.C2);
C3 = findViewById(R.id.C3);
//mapping checkbox and string for ease of use during sharedprefs
Map<String, CheckBox> checkBoxMap = new HashMap();
checkBoxMap.put(C1key,C1);
checkBoxMap.put(C2key,C2);
checkBoxMap.put(C3key,C3);
//loading initial values from sharedprefs, and also creating onCheckedChangeListeners from the map
loadInitialValues(checkBoxMap);
setupCheckedChangeListener(checkBoxMap);
}
public void loadInitialValues(Map<String, CheckBox> checkboxMap) {
for (Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
boolean checked = shp.getBoolean(checkboxEntry.getKey(), false);
checkboxEntry.getValue().setChecked(checked);
}
}
public void setupCheckedChangeListener(Map<String, CheckBox> checkboxMap) { //for loop to cover all the checkboxes and keys in the map
for (final Map.Entry<String, CheckBox> checkboxEntry: checkboxMap.entrySet()) {
checkboxEntry.getValue().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences.Editor editor = shp.edit();
editor.putBoolean(checkboxEntry.getKey(), isChecked);
editor.apply();
// this part is to turn the background of the checkbox to a specified drawable when its checked and when it isn't
if(checkboxEntry.getValue().isChecked()) //checkboxentry.getvalue().ischecked is to check whether specific checkboxes are in the checked state or not, Ex C1.ischecked() C2.ischecked() and so on
{
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
}
else
{
checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background_default);
}
}
});
}
3
Answers
Thanks to Suehtam for helping me out with this really appreciate it!!! this the solution
Humm, if i understood what you want to do is not to save the value but instead to set the box selected.
something like this:
if is not the state selected and is just the drawable you could do the same:
It’s just an example since i don’t know which drawable function you are going to use.
Ex:
Than i believe should be something like this: