How make a ListPreference with checkbox

Tek Yin picture Tek Yin · Nov 3, 2010 · Viewed 13.2k times · Source

How can I create a ListPreference with checkbox? I know how to use ListPreference, but I need multiple selection like in Alarm application on "repeat" preference.

like this screenshot:

checkboxes

Answer

TouchBoarder picture TouchBoarder · Feb 27, 2013

Since API 11 you can use MultiSelectListPreference

String[] selections = {"selection1","Selection2"};
Set<String> selectionSet = new HashSet<String>();
selectionSet.addAll(Arrays.asList(selections));

MultiSelectListPreference multiSelectPref = new MultiSelectListPreference(this);
        multiSelectPref.setKey("multi_pref");
        multiSelectPref.setTitle("Multi Select List  Preference");
        multiSelectPref.setEntries(selections);
        multiSelectPref.setEntryValues(selections);
        multiSelectPref.setDefaultValue(selectionSet);
        getPreferenceScreen().addPreference(multiSelectPref);