Dynamic ListPreference in android
35,991
Solution 1
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);
Solution 2
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"};
Related videos on Youtube
Author by
kkk
Updated on September 04, 2020Comments
-
kkk almost 4 years
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?
-
ccpizza over 11 yearsConsidering 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: stackoverflow.com/questions/6474707/…
-
-
Spacen Jasset almost 13 yearsfindViewById works with views. ListPreference isn't a view, so the second example doesn't seem to work. Can't cast.
-
Boris van Schooten almost 13 yearsSee this example code for dynamically creating other kinds of preferences: developer.android.com/resources/samples/ApiDemos/src/com/…
-
Greg Dan almost 13 yearsfindPreference should work instead of findViewById. However findPreference is deprecated in Honeycomb.
-
Malachi almost 12 yearsA little strange that getEntries and getEntryValues are DOA but I'm sure there's a good reason. And this is a great solution.
-
Jeff G almost 12 yearsBoris - 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-<version>\ApiDemos then follow the rest of Boris's path)
-
Warpzit over 9 yearsSamples path: sdk\samples\android-21\legacy\ApiDemos\src\com\example\android\apis\preference