I would like to know how to use MultiSelectListPreference preference in code.
The following code is taken from the API examples file PreferencesFromCode.java, if someone could give a similar example for MultiSelectListPreference you would make my day 🙂
// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);
I couldn’t find any examples online, but I put this together and it works.
Edit: This solution will only work on ICS+. Honeycomb completely ignores .setValues() and the HashSet passed to the listener contains different values. This is a known bug, but I hope this will help people wanting to implement in Android v4+