MultiChoice Preference Widget for Android

 

Thanks:http://blog.350nice.com/wp/archives/240

MultiChoice Preference Widget for Android


Strangely, the Android framework does not include a widget which allows multiple selections in Preference Screens. I needed to add one of these for selecting folders to monitor for new media in  PicPush   and ended up having to implement it myself. If this is something you need for your Android app, following this tutorial will have you up and running in under 5 minutes.

 

Step 1: Declare a multi-choice preference widget in XML

The first step involves adding the appropriate XML to the file where your preference screen is defined. This can be done as so (You’ll have to change the undefined references to @string constants):

            <com.threefiftynice.android.preference.ListPreferenceMultiSelect android:defaultValue="#ALL#"
                android:dependency="@string/pref_auto_scan_image"
                android:key="@string/pref_upload_buckets_image"
                android:title="Image folders to monitor" android:dialogTitle="Image folders to monitor"
                android:summary="Specify which folders should be monitored for images"
                android:entries="@array/pref_upload_buckets_default_entries"
                android:entryValues="@array/pref_upload_buckets_default_values"/>

Step 2: Create Java class which implements the Multiple Choice List Preference

Now, of course, we need a Java class to back this up. The implementation is straightforward. It extends the ListPreference widget so it can inherit all the XML attributes recognized by it. From there, a few key methods are overridden.  Source code can be downloaded from here.

Step 2.1: Override onPrepareDialogBuilder (line 44)

onPrepareDialogBuilder is the method responsible for creating the popup dialog which contains all the choices to be displayed. The change from the base class here is simply to set the multi choice entries of the builder rather than the single choice entries. This takes care of the UI.

Step 2.2: Override onDialogClosed (line 69)

When the dialog is closed we need to build a special string to store in the SharedPreferences. The string is made up of the selected entries separated by a special separator string. The separator string i chose is quite weird, it has to be, to avoid conflicts with actual entries.

Step 2.3: Provide a static convenience method for parsing the stored value into a String array (line 62)

When a client reads back the stored value from the SharedPreferences it wont be very useful unless it can be parsed. This can be delegated to our custom implementation via a static method called parseStoredValue. This convenience method returns a String array which contains the user’s choices.

Step 3: You’re Done! Run the code!

Following the above steps should have you in good shape to get up and running. I implemented this code a few days ago, and am writing this tutorial from memory so i may have missed a few small details, however the main bones of the implementation is definitely contained here. Any problems/questions? Leave a comment below.

Walla!

Walla!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值