Android: MultiSelectListPreference for SDK < 11

Supporting old versions of Android is getting more and more headache nowadays. Happily, there is a pretty strong community, which continue to contribute various solutions.

In my project, I wanted to use MultiSelectListPreference in order to give a user possibility to select a number of options.
It turned out to be, that Android with SDK less than 11 doesn’t support this class. It means, that all Android devices with OS version less than 4.0 will not have this feature.

Searching Internet helps a lot and so was this time.
I found a pretty interesting solution created by Krzysztof Suszyński
Here is the link to github https://gist.github.com/cardil/4754571

I used the code and it really worked nice for me.
Except, I needed to change the code of the MultiSelectListPreference implementation:

the line “entryChecked = new boolean[getEntries().length];” should be moved from Class initialization to onPrepareDialogBuilder
and sjould look as following:

this is done in order to make dynamic MultiSelectListPreference working.
So, the whole code in my case looked as following:

NOTE: I changed the name of the class in order to use both variants – new and legacy ones.
and now, the implementation of the code.

First, I created Preferences class in order to select proper settings:

I copied class join function and DEFAULT_SEPARATOR from Krzysztof’s code and made it public in order to use it later in my code.

And here we go – usage of saved preferences in my code:

As a result in both cases we get the same result – Set of Strings.