PreferenceFragmentCompat

(SESL variant) A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device. To retrieve an instance of android.content.SharedPreferences that the preference hierarchy in this fragment will use by default, call getDefaultSharedPreferences with a context in the same package as this fragment.

You can define a preference hierarchy as an XML resource, or you can build a hierarchy in code. In both cases you need to use a PreferenceScreen as the root component in your hierarchy.

To inflate from XML, use the setPreferencesFromResource. An example example XML resource is shown further down.

To build a hierarchy from code, use createPreferenceScreen to create the root PreferenceScreen. Once you have added other Preferences to this root screen with addPreference, you then need to set the screen as the root screen in your hierarchy with setPreferenceScreen.

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

Sample Code

The following sample code shows a simple settings screen using an XML resource. The XML resource is as follows:

{@sample samples/SupportPreferenceDemos/src/main/res/xml/preferences.xml preferences}

The fragment that loads the XML resource is as follows:

{@sample samples/SupportPreferenceDemos/src/main/java/com/example/androidx/preference * /Preferences.java preferences}

See also

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog.
Link copied to clipboard
Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment.
Link copied to clipboard
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.

Properties

Link copied to clipboard
val ARG_PREFERENCE_ROOT: String = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"
Fragment argument used to specify the tag of the desired root PreferenceScreen object.

Functions

Link copied to clipboard
open fun addPreferencesFromResource(@XmlRes preferencesResId: Int)
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
Link copied to clipboard
Finds a Preference with the given key.
Link copied to clipboard
A wrapper for getParentFragment which is v17+.
Link copied to clipboard
Link copied to clipboard
Returns the PreferenceManager used by this fragment.
Link copied to clipboard
Gets the root of the preference hierarchy that this fragment is showing.
Link copied to clipboard
open fun needToRefeshSwitch(@NonNull preferenceGroupAdapter: PreferenceGroupAdapter, isLargeLayout: Int, swDp: Int): Boolean
Link copied to clipboard
Link copied to clipboard
open fun onCreate(@Nullable savedInstanceState: Bundle)
Link copied to clipboard
abstract fun onCreatePreferences(@Nullable savedInstanceState: Bundle, @Nullable rootKey: String)
Called during onCreate to supply the preferences for this fragment.
Link copied to clipboard
open fun onCreateRecyclerView(@NonNull inflater: LayoutInflater, @NonNull parent: ViewGroup, @Nullable savedInstanceState: Bundle): RecyclerView
Creates the RecyclerView used to display the preferences.
Link copied to clipboard
open fun onCreateView(@NonNull inflater: LayoutInflater, @Nullable container: ViewGroup, @Nullable savedInstanceState: Bundle): View
Link copied to clipboard
open fun onDestroyView()
Link copied to clipboard
Called when a preference in the tree requests to display a dialog.
Link copied to clipboard
open fun onNavigateToScreen(@NonNull preferenceScreen: PreferenceScreen)
Called by onClick in order to navigate to a new screen of preferences.
Link copied to clipboard
Called when a preference in the tree rooted at this has been clicked.
Link copied to clipboard
open fun onSaveInstanceState(@NonNull outState: Bundle)
Link copied to clipboard
open fun onStart()
Link copied to clipboard
open fun onStop()
Link copied to clipboard
open fun onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle)
Link copied to clipboard
open fun scrollToPreference(@NonNull preference: Preference)
Link copied to clipboard
open fun seslSetRoundedCorner(enabled: Boolean)
Link copied to clipboard
open fun setDivider(@Nullable divider: Drawable)
Sets the Drawable that will be drawn between each item in the list.
Link copied to clipboard
open fun setDividerHeight(height: Int)
Sets the height of the divider that will be drawn between each item in the list.
Link copied to clipboard
open fun setPadding(left: Int, top: Int, right: Int, bottom: Int)
Link copied to clipboard
open fun setPreferenceScreen(preferenceScreen: PreferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing.
Link copied to clipboard
open fun 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.