Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make a ListPreference with checkbox

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

like image 923
Tek Yin Avatar asked Nov 03 '10 08:11

Tek Yin


3 Answers

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);
like image 122
TouchBoarder Avatar answered Nov 13 '22 00:11

TouchBoarder


Use, MultiSelectListPreference

<MultiSelectListPreference
        app:defaultValue="@array/watermark_entries_view"
        app:dialogTitle="Select Watermark Type"
        app:entries="@array/watermark_entries_view"
        app:entryValues="@array/watermark_entries_values"
        app:key="mode_repeat"
        app:summary="Enable Watermark"
        app:title="Watermark" />
like image 27
Ask Shiwa Avatar answered Nov 13 '22 00:11

Ask Shiwa


There is no built-in preference for that AFAIK. ListPreference is single-select only.

You could create your own custom Preference class, though, by extending DialogPreference.

like image 1
CommonsWare Avatar answered Nov 12 '22 22:11

CommonsWare