Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:05+00:00 2026-06-15T15:22:05+00:00

I would like to know how to use MultiSelectListPreference preference in code. The following

  • 0

I would like to know how to use MultiSelectListPreference preference in code.

The following code is taken from the API examples file PreferencesFromCode.java, if someone could give a similar example for MultiSelectListPreference you would make my day 🙂

// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:22:07+00:00Added an answer on June 15, 2026 at 3:22 pm

    I couldn’t find any examples online, but I put this together and it works.

    Edit: This solution will only work on ICS+. Honeycomb completely ignores .setValues() and the HashSet passed to the listener contains different values. This is a known bug, but I hope this will help people wanting to implement in Android v4+

        MultiSelectListPreference listPreference = new MultiSelectListPreference(context);
        listPreference.setTitle(R.string.configure_category_title);
        listPreference.setDialogTitle(R.string.configure_category_title);
        listPreference.setSummary(R.string.configure_category_summary);
        listPreference.setEntries(R.array.configure_category_array);
        listPreference.setEntryValues(new CharSequence[]{
                ProcessList.PREF_SERVICES + mAppWidgetId,
                ProcessList.PREF_INACTIVE + mAppWidgetId,
                ProcessList.PREF_INTERNAL + mAppWidgetId
        });
    
        //Create a Set<String> with list items that should be selected
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        boolean showServices = sharedPref.getBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, true);
        boolean showInactive = sharedPref.getBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, true);
        boolean showInternal = sharedPref.getBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, true);
    
        String[] strings = new String[3];
        int cnt = 0;
        if (showServices)
            strings[cnt++] = ProcessList.PREF_SERVICES + mAppWidgetId;
        if (showInactive)
            strings[cnt++] = ProcessList.PREF_INACTIVE + mAppWidgetId;
        if (showInternal)
            strings[cnt] = ProcessList.PREF_INTERNAL + mAppWidgetId;
    
        Set<String> mySet = new HashSet<String>();
        Collections.addAll(mySet, strings);
    
        //Add the set
        listPreference.setValues(mySet);
    
        //Listen for changes, I'm not sure if this is how it's meant to work, but it does :/
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object o) {
    
                HashSet hashSet = (HashSet) o;
                Iterator stringIterator = hashSet.iterator();
                boolean[] states = {false, false, false};
                String prefString;
    
                while (stringIterator.hasNext()) {
    
                    prefString = (String) stringIterator.next();
    
                    if (prefString == null)
                        continue;
    
                    if (prefString.compareTo(ProcessList.PREF_SERVICES + mAppWidgetId) == 0)
                        states[0] = true;
                    else if (prefString.compareTo(ProcessList.PREF_INACTIVE + mAppWidgetId) == 0)
                        states[1] = true;
                    else if (prefString.compareTo(ProcessList.PREF_INTERNAL + mAppWidgetId) == 0)
                        states[2] = true;
    
                }
    
                PreferenceManager
                        .getDefaultSharedPreferences(getActivity())
                        .edit()
                        .putBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, states[0])
                        .putBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, states[1])
                        .putBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, states[2])
                        .commit();
    
                return true;
            }
        });
    
        preferenceCategory.addPreference(listPreference);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if it is safe to use the following code
i would like to know how to use the java.util.logging api, in order to
I would like to know what code to use to convert a double[] array
I would like to know how could I use JSON for passing data from
I would like to know if I can use the following JQuery function on
I would like to know how to use Converters in Java Server Faces similar
I would like to know how to use jsp:setProperty in the following scenario. Here
I would like to know how to use multiples clas in Java. I know
I would like to know how to use the following fql query to get
i would like to know how to use a DocumentListener/DocumentEvent in java to prevent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.