44

How to generate dynamic listPreference in android? I want to get all wifi access points and make a list using in preference Activity(i.e. make a list using listpreference). How to do this?

kkk
  • 441
  • 1
  • 4
  • 3
  • 3
    Considering that the items will be dynamic it is not enough to set the items in `onCreate`. You also need to update the values when the ListPreference is clicked: http://stackoverflow.com/questions/6474707/how-to-fill-listpreference-dynamically-when-onpreferenceclick-is-triggered/13828912#13828912 – ccpizza Dec 11 '12 at 22:01

3 Answers3

27

Every XML element in Android can be created programmatically as the element name is also a Java class. Hence you can create a ListPreference in code:

CharSequence[] entries = { "One", "Two", "Three" };
CharSequence[] entryValues = { "1", "2", "3" };
ListPreference lp = new ListPreference(this);
lp.setEntries(entries);
lp.setEntryValues(entryValues);

You could alternatively create it in XML then add the entries/entry values in code:

CharSequence[] entries = { "One", "Two", "Three" };
CharSequence[] entryValues = { "1", "2", "3" };
ListPreference lp = (ListPreference)findPreference("list_key_as_defined_in_xml");
lp.setEntries(entries);
lp.setEntryValues(entryValues);
cbrulak
  • 15,436
  • 20
  • 61
  • 101
Philio
  • 3,675
  • 1
  • 23
  • 33
  • 8
    findViewById works with views. ListPreference isn't a view, so the second example doesn't seem to work. Can't cast. – Spacen Jasset Aug 08 '11 at 16:12
  • findPreference should work instead of findViewById. However findPreference is deprecated in Honeycomb. – Greg Dan Sep 03 '11 at 09:20
26

For creating a dynamic list preference, u need to create a preference activity (ie to extend an activity as PreferenceActivity).

The following code can be used to create the list dynamically.

// Root
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
        dialogBasedPrefCat.setTitle("Category Title");
        root.addPreference(dialogBasedPrefCat); //Adding a category

 // List preference under the category
        ListPreference listPref = new ListPreference(this);
        listPref.setKey("keyName"); //Refer to get the pref value
        listPref.setEntries("Array of values");
        listPref.setEntryValues("Array of item value");
        listPref.setDialogTitle("Dialog Title"); 
        listPref.setTitle("Title");
        listPref.setSummary("Summary");
        dialogBasedPrefCat.addPreference(listPref); Adding under the category

        return root;

Hope this helps to get an !dea...

EDIT:

Create and add values to CharSequence[] like this:

CharSequence[] cs = new String[]{"myValue"};
Czechnology
  • 14,832
  • 10
  • 62
  • 88
Dijo David
  • 6,175
  • 11
  • 35
  • 46
  • 6
    See this example code for dynamically creating other kinds of preferences: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/preference/PreferencesFromCode.html – Boris van Schooten Aug 26 '11 at 17:49
  • A little strange that getEntries and getEntryValues are DOA but I'm sure there's a good reason. And this is a great solution. – Malachi Aug 19 '12 at 06:20
  • 1
    Boris - your link doesn't work: it just takes me to the Download Samples index. However, I followed the path indicated when hovering over your link in my downloaded samples and found it. (For anyone else: start from android-sdk\samples\android-\ApiDemos then follow the rest of Boris's path) – Jeff G Sep 23 '12 at 10:18
  • Samples path: sdk\samples\android-21\legacy\ApiDemos\src\com\example\android\apis\preference – Warpzit Dec 08 '14 at 12:21
0

This minimalist technique is for both environments.

In preferences.xml

<!-- NB: Dynamic array insertion for 'entries'/'entryValues' -->
<ListPreference
    android:key="xyzzy"
    android:title="..."
    android:summary="..."
    android:numeric="integer"
    android:defaultValue="0"
    android:layout="?PrefLayoutDtl" 
/>

In PreferenceFragment.onCreate()

addPreferencesFromResource(R.xml.preferences);
expand_xyzzy((ListPreference)findPreference("xyzzy"));

Elsewhere

public static Preference expand_xyzzy (ListPreference pref) {
    if (pref == null) return pref;
    pref.setEntries(new String["one","two","three];
    pref.setEntryValues(new String["0","1","2"]);
    return pref;
}

Notes:
(a) XML is self-documenting and perhaps a better choice than dynamic preference creation.
(b) Starting your PreferenceFragment by NOT using PreferenceActivity easily lets you do this:

image

Bad Loser
  • 3,065
  • 1
  • 19
  • 31