Dynamic ListPreference in android

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

+43
android android-preferences
Mar 21 2018-11-11T00:
source share
3 answers

Each XML element in Android can be created programmatically because the element name is also a Java class. Therefore, you can create a ListPreference in the code:

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

You can also create it in XML and then add entries / input values ​​to the 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); 
+26
Mar 21 2018-11-11T00:
source share

To create a dynamic list preference, you need to create a preference activity (i.e. extend the action as a PreferenceActivity).

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

 // 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 get dea ...

EDIT:

Create and add values ​​to CharSequence [] as follows:

 CharSequence[] cs = new String[]{"myValue"}; 
+26
Mar 21 2018-11-11T00:
source share

This minimalist method is designed 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")); 

In the other place

 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 creating dynamic preferences.
(b) Running PreferenceFragment using NOT with PreferenceActivity allows you to:

image

0
Sep 04 '17 at 1:35 on
source share



All Articles