Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi select ListPreference on android

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

like image 690
zsniperx Avatar asked Aug 29 '10 00:08

zsniperx


3 Answers

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.

like image 70
Tim Avatar answered Nov 19 '22 19:11

Tim


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>

like image 42
Tapan Thaker Avatar answered Nov 19 '22 17:11

Tapan Thaker


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

like image 8
zsniperx Avatar answered Nov 19 '22 17:11

zsniperx