skip to Main Content

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);
                    }

                }
            });
        }

    

enter image description here

3

Answers


  1. Chosen as BEST ANSWER

    Thanks to Suehtam for helping me out with this really appreciate it!!! this the solution

    private 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);
                    if (checkboxEntry.getValue().isChecked()) {
                        checkboxEntry.getValue().setSelected(true);
                        checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
                    }else{
                        //If is not selected
                        checkboxEntry.getValue().setBackgroundResource(0);
                    }
                }
            }
        
        
            private void setupCheckedChangeListener(Map<String, CheckBox> checkboxMap) {
                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);
                            if (checkboxEntry.getValue().isChecked()) {
                                checkboxEntry.getValue().setSelected(true);
                                checkboxEntry.getValue().setBackgroundResource(R.drawable.cb_background);
                            }else{
                                //If is not selected
                                checkboxEntry.getValue().setBackgroundResource(0);
                            }
                            editor.apply();
        
                        }
                    });
                }
        
            }
    

  2. 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(C1.isChecked()){
       //In this way the box would be with the selected view.
       C1.setSelected(true)
    }
    

    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:

    if(C1.isChecked()){
       C1.setCheckMarkDrawable([drawable location]);
    }
    
    Login or Signup to reply.
  3. Than i believe should be something like this:

        //If is selected set the background
        if (C1.isChecked()) {
            C1.setSelected(true);
            C1.setBackgroundResource(R.drawable.[name of the background]);
        }else{
            //If is not selected
            C1.setBackgroundResource(0);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search