23

In my setting page I have a preference which fetches a list of toggle-able settings that I wanted to display as individual checkbox preferences.

I know that preferences.xml supports generating lists of preferences (looking at wi-fi settings page) but ListPreference only allows you to select one from the list.

I've been searching for how to generate preferences programmatically but have only managed to find how to change attributes of preferences that are already in the XML.

Cœur
  • 37,241
  • 25
  • 195
  • 267
isep
  • 703
  • 1
  • 9
  • 24
  • 2
    If you only need multi choice list, take a look at [MultiSelectListPreference](http://developer.android.com/reference/android/preference/MultiSelectListPreference.html) – Jokahero May 25 '11 at 18:58

3 Answers3

53

Here is a short example (assuming you are extending PreferenceActivity):

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);

    PreferenceCategory category = new PreferenceCategory(this);
    category.setTitle("category name");

    screen.addPreference(category);

    CheckBoxPreference checkBoxPref = new CheckBoxPreference(this);
    checkBoxPref.setTitle("title");
    checkBoxPref.setSummary("summary");
    checkBoxPref.setChecked(true);

    category.addPreference(checkBoxPref);
    setPreferenceScreen(screen);
}
inazaruk
  • 74,247
  • 24
  • 188
  • 156
2

Programatically add a preference, with other preferences in xml file: Other solutions didn't work for me because I ALSO had an xml with preferences. I'm not sure all these calls are necessary/redundant, but this works.

onCreate() method, class extends PreferenceActivity:

setContentView(R.layout.preferences);
addPreferencesFromResource(R.xml.preferences);

PreferenceScreen pScreen = getPreferenceManager().createPreferenceScreen(this);     
CheckBoxPreference cb = new CheckBoxPreference(this);
cb.setKey("cb");
cb.setTitle("BLAH");
cb.setOrder(99);        //not working...
pScreen.addPreference(cb);

setPreferenceScreen(pScreen);
addPreferencesFromResource(R.xml.preferences);

Sidenote: Since I needed to generate a dynamic checkbox list, it was best suited inside an inner PreferenceScreen. So I created this PreferenceScreen inside the xml, then dynamically generated the checkboxes inside this. This way the ordering didn't matter since all the 'new' dynamica checkboxes were inside this screen.

Kevin
  • 2,296
  • 21
  • 22
  • mixing preferences from xml and programmatically is very confusing. All preferences can be created programatically and personally I think the programmatic approach is far more convenient then the xml method – slinden77 Oct 09 '16 at 20:41
  • How is one supposed to create a "sub"-PreferenceScreen? You present this code as creating one, but it really uses the main one and adds a Category at the very top. There isn't much to adapt this. :/ – payne Aug 22 '18 at 18:51
0

Try the below code.

CheckBoxPreference checkBoxPref = findPreference("your_key");
if (checkBoxPref != null) 
{
 checkBoxPref.setChecked(false);
}
HAZEEM JOONUS
  • 483
  • 6
  • 9