ListPreference with a max number of selectable options

I want to have an element in my preference menu that does the following:

  • Show a list of options.
  • Many are selectable
  • Maximum amount of options to be chosen 2.

Possibilities I thought of:

  1. Doing a separated PreferenceScreen and showing options as checkBoxes, but I don't know where to place the logic of max 2 options.
  2. Extending DialogPreference and doing it by hand.

What's the best way?


Asked by: Paul319 | Posted: 20-01-2022






Answer 1

Extending DialogPreference would get you the closest in terms of look-and-feel; the Preference classes are fairly unflexible and un-extendable in my experience.

I can't remember too much about PreferenceScreen, but I imagine it's similar.

In an app I worked on, we ended up using separate activities, launched via Intent from a Preference item onClick. This allowed us to easily develop preference screens that require validation logic a bit more complex than the usual.

Answered by: Elise554 | Posted: 21-02-2022



Answer 2

You can put the logic of maximum two options in a OnSharedPreferenceChangeListener.

So you just listen to all the preferences as they change and update them if an invalid combination is selected.

So your code would be something like the following:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
    //Code to calcuate how many are selected
    int code = numberSelected();

    if (count > 2) {
        sharedPreferences.edit().putBoolean(key,false).commit();
        Toast.makeText(this,"Can't select more than two!",Toast.LENGTH_LONG).show();
    }
}

If you create your own PreferenceActivity that implements OnSharedPreferenceChangeListener you can enable the listener to be listening only when required doing something like this:

@Override
protected void onResume() {
super.onResume();
    //Register the listener
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    // Unregister the listener
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

Answered by: Justin643 | Posted: 21-02-2022



Similar questions

android: using ListPreference and retrieving key string

I have a settings menu that pops up and in it is a ListPreference type menu. It is associated with a settings.xml file where there are 'array-strings' within it. It all works good but I don't know how to retrieve the users preference. As an example, let's say the user picks a color (red, green, or blue). The list that I've made within my 'array-strings' contain the text red, green, and blue. Within my code, I would...


How to get an Android ListPreference defined in Xml whose values are integers?

Is it possible to define a ListPreference in Xml and retrieve the value from SharedPreferences using getInt? Here is my Xml: <ListPreference android:key="@string/prefGestureAccuracyKey" android:title="@string/prefGestureAccuracyTitle" android:summary="@string/prefGestureAccuracyDesc" android:entries="@array/prefNumberAccuracyLabels" android:entryValues="@array/prefNumberAccuracyValues" androi...


android - Account preferences crashes on ListPreference

I have created an account type using the AccountAuthenticator stuff as done in the SampleSyncAdapter tutorial. I am now trying to get account preferences working. I have added the line android:accountPreferences="@xml/account_preferences" to my account-authenticator and account_preferences.xml looks like so: <?xml version="1.0" encoding="utf-8"?> <Pre...


listpreference - With android I want to have 2 preferences with the same key

I have 2 prefs - a song title and the audio state(mute or volume) that I want to store in the same key. This works for the clicks and the only problem I have is resetting the summary on onSharedPreferencesChanged I get errors with this. PreferenceScreen musicPrefScreen = (PreferenceScreen)getPreferenceScreen ().findPreference("theme" + Team_ID); musicPrefScreen.setSummary(mPreferences.getString("t...


How to edit a ListPreference value from java code in Android

I know how to change the value of a CheckBoxPreference, but I can't get it to work for a ListPreference. Here is how my preference screen is built: one CheckBox for the default A ListPreference to select a color other than the default. The ListPreference is defined with the key "titleColor", as follows :


Easy way to make the ListPreference multiple choice in Android?

I am trying to create a preference dialog window that allows the user to select more then one item in the list. Currently it only allows you to select one item. Is there an easy way to do this? I have looked all over the internet and not seen a way as of yet. Any help is appreciated!


java - Simple ListPreference is not working. Any ideas?

Could someone maybe tell me what i'm doing wrong? I'm betting im missing one small thing. I've looked on the developer site and i've read some tutorials and i'm just not seeing what i did wrong. I'm trying to use a ListPreference to decide which sound to play on a button click. I have this at the top: public String greensound; Here's my OnClick code: case R....


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


android - ListPreference is throwing a strange null pointer exception

I have an app that obtains a list of available Calendars and displays them in the Spinner. I have checks to ensure that if the available Calendar list is empty, then ListPreference will still display "None available" in the Spinner. I am using the ListPreference to display the Spinner in the Preferences tab. Here is the preferences.xml: <ListPreference android:title="Calendar" and...


android - Refresh a ListPreference

I have a ListPreference which I populate dynamically when clicking on the list to display it. The population works fine, but the values I populate is not displayed until the next time i click to open the list, but instead the values from the xml-file is displayed the first time. It feels like the list has already been populated before the onPreferenceClick is called. How can I refresh the list probably? I want to populate ...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top