Multi select ListPreference on android
Asked Answered
S

5

10

Any idea on implementing a multi-select (check boxes) ListPreference on Android?

Would I have to extend the ListPreference?
Is there any classes already documented to do this?

Thanks

Sharonsharona answered 29/8, 2010 at 0:21 Comment(0)
S
8

Found a very useful link: http://blog.350nice.com/wp/archives/240

Sharonsharona answered 29/8, 2010 at 1:53 Comment(4)
The link is dead. Can the content still be found somewhere?Erick
found this cached page from google if it's help: webcache.googleusercontent.com/search?q=cache:http://…Cubiform
Do not simply add links as answers; links can expire. Instead please add important block from the link along with it.Canister
Internet Wayback Machine has a capture of it: https://web.archive.org/web/20100406030947/http://blog.350nice.com/wp/archives/240, with the source code and everything.Ply
F
22

Multi select ListPreference now comes natively with Android from API level 11 (Honeycomb). http://developer.android.com/reference/android/preference/MultiSelectListPreference.html

Because it will be quite a while before devices have Honeycomb or later installed I'd recommend people to stick with the http://blog.350nice.com/wp/archives/240 solution.

EDIT: I think at this moment in time (almost 3 years after this answer was originally posted) you are better off using the native version now as the majority of devices have Android 4 and up.

Feoffee answered 23/2, 2011 at 13:39 Comment(1)
Many developers, myself included, would prefer not to lock out a still significant segment of users by increasing our minSdkVersion from 10 to 11. What I don't understand is what is the need for all these solutions, github projects posted in other answers, etc? Are we not forgetting that Android is open source. If you want to target API 10+, just grab the actual source code for the MultiSelectListPreference and be done with it, e.g. android.googlesource.com/platform/frameworks/base/+/cd92588/…Conformation
V
10

Well , http://blog.350nice.com/wp/archives/240 does provide a solution , but a simpler solution would be just implementing a child preference screen inside the parent , and then the child preference screen can have multiple check boxes . I know , its not the best solution , but gets the job done .

For eg - the Below preference.xml

<PreferenceCategory 
    android:title="Regular messages"
    android:key="regular_messages">

    <CheckBoxPreference 
        android:key="enable_regular_messages"
        android:summary="Enable or disable regular messages"
        android:title="Send regular messages" 
        android:defaultValue="true"
    />

    <ListPreference 
        android:key="send_interval"
        android:title="Send interval"
        android:summary="Define how often you want to send messages"
        android:defaultValue="60000" 
        android:entries="@array/send_interval"
        android:entryValues="@array/send_interval_values"
        android:dependency="enable_regular_messages"
    />

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
     android:title="Messages type"
     android:key="messages_type"
     android:summary="Select the type of messages to be sent"
     android:dependency="enable_regular_messages">
    <CheckBoxPreference 
        android:key="enable_status_messages"
        android:summary="Enable or disable status messages"
        android:title="Send status messages" 
        android:defaultValue="true"
    />

    <CheckBoxPreference 
        android:key="enable_event_messages"
        android:summary="Enable or disable event messages"
        android:title="Send event messages" 
        android:defaultValue="true"
    />

    <CheckBoxPreference 
        android:key="enable_critical_messages"
        android:summary="Enable or  disable critical messages"
        android:title="Send critical messages" 
        android:defaultValue="true"
    />

    </PreferenceScreen>

</PreferenceCategory>

Vivienviviene answered 21/11, 2011 at 23:34 Comment(0)
S
8

Found a very useful link: http://blog.350nice.com/wp/archives/240

Sharonsharona answered 29/8, 2010 at 1:53 Comment(4)
The link is dead. Can the content still be found somewhere?Erick
found this cached page from google if it's help: webcache.googleusercontent.com/search?q=cache:http://…Cubiform
Do not simply add links as answers; links can expire. Instead please add important block from the link along with it.Canister
Internet Wayback Machine has a capture of it: https://web.archive.org/web/20100406030947/http://blog.350nice.com/wp/archives/240, with the source code and everything.Ply
L
4

Here's a single-class implementation with defaultValue support:
https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/MultiSelectListPreference.java

Latif answered 5/5, 2012 at 19:29 Comment(1)
It works well. I would add this method to the code, so that you don't need to import it: public static boolean isEmpty(CharSequence str) { return str == null || str.length() == 0; }Polyhedron
C
1

There is a github project just for this

Cubiform answered 17/6, 2013 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.