Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 1.0.0
belongs to Maven artifact androidx.preference:preference:1.0.0-rc01

PreferenceFragment

public abstract class PreferenceFragment
extends Fragment implements PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment

java.lang.Object
   ↳ android.app.Fragment
     ↳ androidx.preference.PreferenceFragment


Shows a hierarchy of Preference objects as lists. These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences 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 PreferenceScreen object should be at the top of the preference hierarchy. Furthermore, subsequent PreferenceScreen in the hierarchy denote a screen break--that is the preferences contained within subsequent PreferenceScreen should be shown on another screen. The preference framework handles this by calling onNavigateToScreen(PreferenceScreen).

The preference hierarchy can be formed in multiple ways:

  • From an XML file specifying the hierarchy
  • From different Activities that each specify its own preferences in an XML file via Activity meta-data
  • From an object hierarchy rooted with PreferenceScreen

    To inflate from XML, use the addPreferencesFromResource(int). The root element should be a PreferenceScreen. Subsequent elements can point to actual Preference subclasses. As mentioned above, subsequent PreferenceScreen in the hierarchy will result in the screen break.

    To specify an object hierarchy rooted with PreferenceScreen, use setPreferenceScreen(PreferenceScreen).

    As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick(Preference).

    Developer Guides

    For information about using PreferenceFragment, read the Settings guide.

    Sample Code

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

    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/root_title">
    
        <Preference
            android:key="basic_preference"
            android:title="@string/title_basic_preference"
            android:summary="@string/summary_basic_preference" />
    
        <Preference
            android:key="stylish_preference"
            android:title="@string/title_stylish_preference"
            android:summary="@string/summary_stylish_preference" />
    
        <Preference
            android:key="preference_with_icon"
            android:title="Preference with icon"
            android:summary="This preference has an icon"
            android:icon="@android:drawable/ic_menu_camera" />
    
        <PreferenceCategory
            android:title="@string/inline_preferences">
    
            <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="@string/title_checkbox_preference"
                android:summary="@string/summary_checkbox_preference" />
    
            <SwitchPreference
                android:key="switch_preference"
                android:title="Switch preference"
                android:summary="This is a switch" />
    
            <DropDownPreference
                android:key="dropdown_preference"
                android:title="@string/title_dropdown_preference"
                android:summary="@string/summary_dropdown_preference"
                android:entries="@array/entries_list_preference"
                android:entryValues="@array/entryvalues_list_preference" />
    
            <SeekBarPreference
                android:key="seekbar_preference"
                android:title="Seekbar preference"
                android:summary="This is a seekbar preference"
                android:max="10"
                android:defaultValue="5"/>
    
        </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" />
    
            <MultiSelectListPreference
                android:key="multi_select_list_preference"
                android:title="@string/title_multi_list_preference"
                android:summary="@string/summary_multi_list_preference"
                android:entries="@array/entries_list_preference"
                android:entryValues="@array/entryvalues_list_preference"
                android:dialogTitle="@string/dialog_title_multi_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>

    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:

    public static class PrefsFragment extends PreferenceFragment {
    
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            // Load the preferences from an XML resource
            setPreferencesFromResource(R.xml.preferences, rootKey);
        }
    }

    Summary

    Nested classes

    interface PreferenceFragment.OnPreferenceDisplayDialogCallback

     

    interface PreferenceFragment.OnPreferenceStartFragmentCallback

    Interface that PreferenceFragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. 

    interface PreferenceFragment.OnPreferenceStartScreenCallback

    Interface that PreferenceFragment's containing activity should implement to be able to process preference items that wish to switch to a new screen of preferences. 

    XML attributes

    PreferenceFragment_android_divider  
    PreferenceFragment_android_dividerHeight  

    Constants

    String ARG_PREFERENCE_ROOT

    Fragment argument used to specify the tag of the desired root PreferenceScreen object.

    Inherited constants

    Public constructors

    PreferenceFragment()

    Public methods

    void addPreferencesFromResource(int preferencesResId)

    Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.

    Preference findPreference(CharSequence key)

    Finds a Preference based on its key.

    final RecyclerView getListView()
    PreferenceManager getPreferenceManager()

    Returns the PreferenceManager used by this fragment.

    PreferenceScreen getPreferenceScreen()

    Gets the root of the preference hierarchy that this fragment is showing.

    void onCreate(Bundle savedInstanceState)
    RecyclerView.LayoutManager onCreateLayoutManager()

    Called from onCreateRecyclerView(LayoutInflater, ViewGroup, Bundle) to create the RecyclerView.LayoutManager for the created RecyclerView.

    abstract void onCreatePreferences(Bundle savedInstanceState, String rootKey)

    Called during onCreate(Bundle) to supply the preferences for this fragment.

    RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)

    Creates the RecyclerView used to display the preferences.

    View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    void onDestroyView()
    void onDisplayPreferenceDialog(Preference preference)

    Called when a preference in the tree requests to display a dialog.

    void onNavigateToScreen(PreferenceScreen preferenceScreen)

    Called by PreferenceScreen.onClick() in order to navigate to a new screen of preferences.

    boolean onPreferenceTreeClick(Preference preference)

    Called when a preference in the tree rooted at this PreferenceScreen has been clicked.

    void onSaveInstanceState(Bundle outState)
    void onStart()
    void onStop()
    void onViewCreated(View view, Bundle savedInstanceState)
    void scrollToPreference(Preference preference)
    void scrollToPreference(String key)
    void setDivider(Drawable divider)

    Sets the drawable that will be drawn between each item in the list.

    void setDividerHeight(int height)

    Sets the height of the divider that will be drawn between each item in the list.

    void setPreferenceScreen(PreferenceScreen preferenceScreen)

    Sets the root of the preference hierarchy that this fragment is showing.

    void setPreferencesFromResource(int preferencesResId, String key)

    Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key.

    Protected methods

    Adapter onCreateAdapter(PreferenceScreen preferenceScreen)

    Creates the root adapter.

    Inherited methods

    XML attributes

    PreferenceFragment_android_divider

    Related methods:

    PreferenceFragment_android_dividerHeight

    Related methods:

    Constants

    ARG_PREFERENCE_ROOT

    added in version 1.0.0
    public static final String ARG_PREFERENCE_ROOT

    Fragment argument used to specify the tag of the desired root PreferenceScreen object.

    Constant Value: "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"

    Public constructors

    PreferenceFragment

    added in version 1.0.0
    public PreferenceFragment ()

    Public methods

    addPreferencesFromResource

    added in version 1.0.0
    public void addPreferencesFromResource (int preferencesResId)

    Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.

    Parameters
    preferencesResId int: The XML resource ID to inflate.

    findPreference

    added in version 1.0.0
    public Preference findPreference (CharSequence key)

    Finds a Preference based on its key.

    Parameters
    key CharSequence: The key of the preference to retrieve.

    Returns
    Preference The Preference with the key, or null.

    getListView

    added in version 1.0.0
    public final RecyclerView getListView ()

    Returns
    RecyclerView

    getPreferenceManager

    added in version 1.0.0
    public PreferenceManager getPreferenceManager ()

    Returns the PreferenceManager used by this fragment.

    Returns
    PreferenceManager The PreferenceManager.

    getPreferenceScreen

    added in version 1.0.0
    public PreferenceScreen getPreferenceScreen ()

    Gets the root of the preference hierarchy that this fragment is showing.

    Returns
    PreferenceScreen The PreferenceScreen that is the root of the preference hierarchy.

    onCreate

    public void onCreate (Bundle savedInstanceState)

    Parameters
    savedInstanceState Bundle

    onCreateLayoutManager

    added in version 1.0.0
    public RecyclerView.LayoutManager onCreateLayoutManager ()

    Called from onCreateRecyclerView(LayoutInflater, ViewGroup, Bundle) to create the RecyclerView.LayoutManager for the created RecyclerView.

    Returns
    RecyclerView.LayoutManager A new RecyclerView.LayoutManager instance.

    onCreatePreferences

    added in version 1.0.0
    public abstract void onCreatePreferences (Bundle savedInstanceState, 
                    String rootKey)

    Called during onCreate(Bundle) to supply the preferences for this fragment. Subclasses are expected to call setPreferenceScreen(PreferenceScreen) either directly or via helper methods such as addPreferencesFromResource(int).

    Parameters
    savedInstanceState Bundle: If the fragment is being re-created from a previous saved state, this is the state.

    rootKey String: If non-null, this preference fragment should be rooted at the PreferenceScreen with this key.

    onCreateRecyclerView

    added in version 1.0.0
    public RecyclerView onCreateRecyclerView (LayoutInflater inflater, 
                    ViewGroup parent, 
                    Bundle savedInstanceState)

    Creates the RecyclerView used to display the preferences. Subclasses may override this to return a customized RecyclerView.

    Parameters
    inflater LayoutInflater: The LayoutInflater object that can be used to inflate the RecyclerView.

    parent ViewGroup: The parent View that the RecyclerView will be attached to. This method should not add the view itself, but this can be used to generate the LayoutParams of the view.

    savedInstanceState Bundle: If non-null, this view is being re-constructed from a previous saved state as given here

    Returns
    RecyclerView A new RecyclerView object to be placed into the view hierarchy

    onCreateView

    public View onCreateView (LayoutInflater inflater, 
                    ViewGroup container, 
                    Bundle savedInstanceState)

    Parameters
    inflater LayoutInflater

    container ViewGroup

    savedInstanceState Bundle

    Returns
    View

    onDestroyView

    public void onDestroyView ()

    onDisplayPreferenceDialog

    added in version 1.0.0
    public void onDisplayPreferenceDialog (Preference preference)

    Called when a preference in the tree requests to display a dialog. Subclasses should override this method to display custom dialogs or to handle dialogs for custom preference classes.

    Parameters
    preference Preference: The Preference object requesting the dialog.

    onNavigateToScreen

    added in version 1.0.0
    public void onNavigateToScreen (PreferenceScreen preferenceScreen)

    Called by PreferenceScreen.onClick() in order to navigate to a new screen of preferences. Calls PreferenceFragment.OnPreferenceStartScreenCallback.onPreferenceStartScreen(PreferenceFragment, PreferenceScreen) if the target fragment or containing activity implements PreferenceFragment.OnPreferenceStartScreenCallback.

    Parameters
    preferenceScreen PreferenceScreen: The PreferenceScreen to navigate to.

    onPreferenceTreeClick

    added in version 1.0.0
    public boolean onPreferenceTreeClick (Preference preference)

    Called when a preference in the tree rooted at this PreferenceScreen has been clicked.

    Parameters
    preference Preference: The preference that was clicked.

    Returns
    boolean Whether the click was handled.

    onSaveInstanceState

    public void onSaveInstanceState (Bundle outState)

    Parameters
    outState Bundle

    onStart

    public void onStart ()

    onStop

    public void onStop ()

    onViewCreated

    public void onViewCreated (View view, 
                    Bundle savedInstanceState)

    Parameters
    view View

    savedInstanceState Bundle

    scrollToPreference

    added in version 1.0.0
    public void scrollToPreference (Preference preference)

    Parameters
    preference Preference

    scrollToPreference

    added in version 1.0.0
    public void scrollToPreference (String key)

    Parameters
    key String

    setDivider

    added in version 1.0.0
    public void setDivider (Drawable divider)

    Sets the drawable that will be drawn between each item in the list.

    Note: If the drawable does not have an intrinsic height, you should also call setDividerHeight(int).

    Related XML Attributes:

    Parameters
    divider Drawable: the drawable to use

    setDividerHeight

    added in version 1.0.0
    public void setDividerHeight (int height)

    Sets the height of the divider that will be drawn between each item in the list. Calling this will override the intrinsic height as set by setDivider(Drawable)

    Related XML Attributes:

    Parameters
    height int: The new height of the divider in pixels.

    setPreferenceScreen

    added in version 1.0.0
    public void setPreferenceScreen (PreferenceScreen preferenceScreen)

    Sets the root of the preference hierarchy that this fragment is showing.

    Parameters
    preferenceScreen PreferenceScreen: The root PreferenceScreen of the preference hierarchy.

    setPreferencesFromResource

    added in version 1.0.0
    public void setPreferencesFromResource (int preferencesResId, 
                    String key)

    Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key.

    Parameters
    preferencesResId int: The XML resource ID to inflate.

    key String: The preference key of the PreferenceScreen to use as the root of the preference hierarchy, or null to use the root PreferenceScreen.

    Protected methods

    onCreateAdapter

    added in version 1.0.0
    protected Adapter onCreateAdapter (PreferenceScreen preferenceScreen)

    Creates the root adapter.

    Parameters
    preferenceScreen PreferenceScreen: Preference screen object to create the adapter for.

    Returns
    Adapter An adapter that contains the preferences contained in this PreferenceScreen.