Android CheckBoxPreference - un/check all preferences
Asked Answered
Z

2

6

I have a PreferenceScreen containing only CheckBoxPreferences (categories to select). I need an option to check/uncheck all of them. I have the following code that does the job but there is one problem: checkboxes on the screen are not updated - I'd need to call some invalidate on the view or something.

Here is my present code:

    private void checkAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, true);
        }
        editor.commit();
    }

    private void uncheckAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, false);
        }
        editor.commit();
        this.restart();
    }

This code works fine but I'd need to refresh the view somehow to see the result imediatelly (not just after closing and re-starting the settings activity).

Thank You all for any advice!

Zephaniah answered 2/5, 2011 at 23:12 Comment(0)
O
14

That's because you're not actually grabbing the preference objects, just the actual values. Try this in your for loops:

 boolean theValue = param;
 for(String s : categories.keySet()) {
    Preference pref = findPreference(s);
    if (pref instanceof CheckBoxPreference) {
      CheckBoxPreference check = (CheckBoxPreference)pref;
      check.setChecked(theValue);
    }
  }

I think you can omit calling the editor and committing, as the setChecked() will do that for you.

Check this out as well.

Overtake answered 3/5, 2011 at 2:38 Comment(3)
Just for information: Although deprecated from HoneyComb this code will work till ICS, doesn't work with Jelly Bean.Tolle
Which part doesn't work with Jelly Bean? I checked findPreference and setChecked and they aren't marked for deprecation as of 4.1.1.Overtake
Good info, thanks. It's possible to simplify the code and to eliminate a variable this way: ((CheckBoxPreference)pref).setChecked(theValue);Surculose
Z
0

Now I have this working code, maybe it will help somebody:

import ...

public class CategoriesActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.category_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.categories_check_all:
                this.checkAll();
                return true;
            case R.id.categories_uncheck_all:
                this.uncheckAll();
                return true;
        }
        return false;
    }

    private void checkAll() {
        this.setCheckState(true);
    }

    private void uncheckAll() {
        this.setCheckState(false);
    }

    private void setCheckState(Boolean state) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for (String s : categories.keySet()) {
            Preference pref = findPreference(s);
            if (pref instanceof CheckBoxPreference) {
                CheckBoxPreference check = (CheckBoxPreference) pref;
                check.setChecked(state);
            }
        }

    }
}
Zephaniah answered 3/5, 2011 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.