Google is committed to advancing racial equity for Black communities. See how.

PreferenceFragment

abstract class PreferenceFragment : Fragment, PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment
kotlin.Any
   ↳ android.app.Fragment
   ↳ androidx.preference.PreferenceFragment

Shows a hierarchy of Preference objects as lists. These preferences will automatically save to android.content.SharedPreferences as the user interacts with them. To retrieve an instance of android.content.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 android.app.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).

  • Summary

    Nested classes
    abstract

    Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog.

    abstract

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

    abstract

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

    Constants
    static String

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

    Public constructors

    Shows a hierarchy of Preference objects as lists.

    Public methods
    open Unit
    addPreferencesFromResource(@XmlRes preferencesResId: Int)

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

    open T?

    Finds a Preference based on its key.

    RecyclerView!

    open PreferenceManager!

    Returns the PreferenceManager used by this fragment.

    open PreferenceScreen!

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

    open Unit
    onCreate(savedInstanceState: Bundle?)

    open RecyclerView.LayoutManager!

    Called from onCreateRecyclerView to create the RecyclerView.LayoutManager for the created RecyclerView.

    abstract Unit
    onCreatePreferences(savedInstanceState: Bundle!, rootKey: String!)

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

    open RecyclerView!
    onCreateRecyclerView(inflater: LayoutInflater!, parent: ViewGroup!, savedInstanceState: Bundle!)

    Creates the RecyclerView used to display the preferences.

    open View?
    onCreateView(inflater: LayoutInflater!, container: ViewGroup?, savedInstanceState: Bundle!)

    open Unit

    open Unit

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

    open Unit

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

    open Boolean

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

    open Unit

    open Unit

    open Unit

    open Unit
    onViewCreated(view: View!, savedInstanceState: Bundle?)

    open Unit

    open Unit

    open Unit
    setDivider(divider: Drawable!)

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

    open Unit

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

    open Unit

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

    open Unit
    setPreferencesFromResource(@XmlRes preferencesResId: Int, @Nullable key: String?)

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

    Protected methods
    open RecyclerView.Adapter<RecyclerView.ViewHolder!>!
    onCreateAdapter(preferenceScreen: PreferenceScreen!)

    Creates the root adapter.

    Constants

    ARG_PREFERENCE_ROOT

    static val ARG_PREFERENCE_ROOT: String

    Deprecated: Use PreferenceFragmentCompat instead

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

    Value: "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"

    Public constructors

    <init>

    PreferenceFragment()

    Deprecated: Use PreferenceFragmentCompat instead

    Shows a hierarchy of Preference objects as lists. These preferences will automatically save to android.content.SharedPreferences as the user interacts with them. To retrieve an instance of android.content.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 android.app.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).

  • Public methods

    addPreferencesFromResource

    open fun addPreferencesFromResource(@XmlRes preferencesResId: Int): Unit

    Deprecated: Use PreferenceFragmentCompat instead

    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