How make a ListPreference with checkbox
Asked Answered
O

4

13

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

Occupation answered 3/11, 2010 at 8:21 Comment(0)
T
21

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);
Tedman answered 27/2, 2013 at 22:36 Comment(0)
H
2

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" />
Hairpin answered 19/9, 2019 at 4:18 Comment(0)
L
1

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.

Layout answered 3/11, 2010 at 8:25 Comment(0)
S
0

For boolean values you must use a SwitchPreference, as follows:

  <SwitchPreference
        android:defaultValue="true"
        android:key="example_switch"
        android:summary="@string/pref_description_social_recommendations"
        android:title="@string/pref_title_social_recommendations" />
Sidney answered 10/6, 2017 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.