Android.Preferences.PreferenceFragment Class
Shows a hierarchy of Android.Preferences.Preference objects as lists.

See Also: PreferenceFragment Members

Syntax

[Android.Runtime.Register("android/preference/PreferenceFragment", DoNotGenerateAcw=true)]
public abstract class PreferenceFragment : Android.App.Fragment

Remarks

Shows a hierarchy of Android.Preferences.Preference objects as lists. These preferences will automatically save to Android.Content.ISharedPreferences as the user interacts with them. To retrieve an instance of Android.Content.ISharedPreferences that the preference hierarchy in this fragment will use, call PreferenceManager.GetDefaultSharedPreferences(Android.Content.Context) with a context in the same package as this fragment.

Furthermore, the preferences shown will follow the visual style of system preferences. It is easy to create a hierarchy of preferences (that can be shown on multiple screens) via XML. For these reasons, it is recommended to use this fragment (as a superclass) to deal with preferences in applications.

A Android.Preferences.PreferenceScreen object should be at the top of the preference hierarchy. Furthermore, subsequent Android.Preferences.PreferenceScreen in the hierarchy denote a screen break--that is the preferences contained within subsequent Android.Preferences.PreferenceScreen should be shown on another screen. The preference framework handles showing these other screens from the preference hierarchy.

The preference hierarchy can be formed in multiple ways: From an XML file specifying the hierarchy From different Android.App.Activity that each specify its own preferences in an XML file via Android.App.Activity meta-data From an object hierarchy rooted with Android.Preferences.PreferenceScreen To inflate from XML, use the PreferenceFragment.AddPreferencesFromResource(int). The root element should be a Android.Preferences.PreferenceScreen. Subsequent elements can point to actual Android.Preferences.Preference subclasses. As mentioned above, subsequent Android.Preferences.PreferenceScreen in the hierarchy will result in the screen break. To specify an Android.Content.Intent to query Android.App.Activity that each have preferences, use PreferenceFragment.AddPreferencesFromIntent(Android.Content.Intent). Each Android.App.Activity can specify meta-data in the manifest (via the key PreferenceManager.MetadataKeyPreferences) that points to an XML resource. These XML resources will be inflated into a single preference hierarchy and shown by this fragment. To specify an object hierarchy rooted with Android.Preferences.PreferenceScreen, use PreferenceFragment.PreferenceScreen.

See Also

As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see PreferenceFragment.OnPreferenceTreeClick(PreferenceScreen, Android.Preferences.Preference).

Developer Guides

Sample Code

xml Example

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="@string/inline_preferences">

        <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="@string/title_checkbox_preference"
                android:summary="@string/summary_checkbox_preference" />

    </PreferenceCategory>

    <PreferenceCategory
            android:title="@string/dialog_based_preferences">

        <EditTextPreference
                android:key="edittext_preference"
                android:title="@string/title_edittext_preference"
                android:summary="@string/summary_edittext_preference"
                android:dialogTitle="@string/dialog_title_edittext_preference" />

        <ListPreference
                android:key="list_preference"
                android:title="@string/title_list_preference"
                android:summary="@string/summary_list_preference"
                android:entries="@array/entries_list_preference"
                android:entryValues="@array/entryvalues_list_preference"
                android:dialogTitle="@string/dialog_title_list_preference" />

    </PreferenceCategory>

    <PreferenceCategory
            android:title="@string/launch_preferences">

        <!-- This PreferenceScreen tag serves as a screen break (similar to page break
             in word processing). Like for other preference types, we assign a key
             here so it is able to save and restore its instance state. -->
        <PreferenceScreen
                android:key="screen_preference"
                android:title="@string/title_screen_preference"
                android:summary="@string/summary_screen_preference">

            <!-- You can place more preferences here that will be shown on the next screen. -->

            <CheckBoxPreference
                    android:key="next_screen_checkbox_preference"
                    android:title="@string/title_next_screen_toggle_preference"
                    android:summary="@string/summary_next_screen_toggle_preference" />

        </PreferenceScreen>

        <PreferenceScreen
                android:title="@string/title_intent_preference"
                android:summary="@string/summary_intent_preference">

            <intent android:action="android.intent.action.VIEW"
                    android:data="http://www.android.com" />

        </PreferenceScreen>

    </PreferenceCategory>

    <PreferenceCategory
            android:title="@string/preference_attributes">

        <CheckBoxPreference
                android:key="parent_checkbox_preference"
                android:title="@string/title_parent_preference"
                android:summary="@string/summary_parent_preference" />

        <!-- The visual style of a child is defined by this styled theme attribute. -->
        <CheckBoxPreference
                android:key="child_checkbox_preference"
                android:dependency="parent_checkbox_preference"
                android:layout="?android:attr/preferenceLayoutChild"
                android:title="@string/title_child_preference"
                android:summary="@string/summary_child_preference" />

    </PreferenceCategory>

</PreferenceScreen>

java Example

public static class PrefsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
}

For information about using PreferenceFragment, read the Settings guide.

The following sample code shows a simple preference fragment that is populated from a resource. The resource it loads is:

The fragment implementation itself simply populates the preferences when created. Note that the preferences framework takes care of loading the current values out of the app preferences and writing them when changed:

[Android Documentation]

Requirements

Namespace: Android.Preferences
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 11