Skip to content Skip to sidebar Skip to footer

How To Dynamically Add Preferences Into Preferences Screen And Bind Their Values?

I'm new in Android. In my app i want to do something like this: I have a container and i want to add item dynamically to it, in one item there may be some fields, so tree would be

Solution 1:

You need to create an xml file with an empty PreferenceScreen:

<?xml version="1.0" encoding="utf-8"?><PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"></PreferenceScreen>

Then in your PreferenceFragmentyou must call the following method in your onCreate(Bundle savedInstanceState) {...}

addPreferencesFromResource(R.xml.pref_empty);

Afterwards you can add Preferences like this:

PreferenceScreenpreferenceScreen=this.getPreferenceScreen();

// create preferences manuallyPreferenceCategorypreferenceCategory=newPreferenceCategory(preferenceScreen.getContext());
preferenceCategory.setTitle("yourTitle");
// do anything you want with the preferencecategory here
preferenceScreen.addPreference(preferenceCategory);

Preferencepreference=newPreference(preferenceScreen.getContext());
preference.setTitle("yourTitle");
// do anything you want with the preferencey here
preferenceCategory.addPreference(preference);

Of course you can add preferences and categories in a loop to add them dynamically.

Post a Comment for "How To Dynamically Add Preferences Into Preferences Screen And Bind Their Values?"