PreferenceFragment
abstract class PreferenceFragment : Fragment, PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment
BaseLeanbackPreferenceFragment |
This class is deprecated. |
LeanbackPreferenceFragment |
This class is deprecated. |
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 getDefaultSharedPreferences 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.
The preference hierarchy can be formed in multiple ways:
- From an XML file specifying the hierarchy
- From different
Activitiesthat each specify its own preferences in an XML file viaandroid.app.Activitymeta-data - From an object hierarchy rooted with
PreferenceScreen
To inflate from XML, use the addPreferencesFromResource. 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.
As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick.
| See also | |
|---|---|
Preference |
|
PreferenceScreen |
Summary
Nested types |
|---|
|
Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. |
|
Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. |
|
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 |
|
|---|---|
const String! |
ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"This property is deprecated. Use |
Public constructors |
|---|
Public functions |
|
|---|---|
Unit |
This function is deprecated. Use |
T! |
<T : Preference?> This function is deprecated. Use |
RecyclerView! |
This function is deprecated. Use |
PreferenceManager! |
This function is deprecated. Use |
PreferenceScreen! |
This function is deprecated. Use |
Unit |
This function is deprecated. |
RecyclerView.LayoutManager |
This function is deprecated. Use |
abstract Unit |
This function is deprecated. Use |
RecyclerView |
This function is deprecated. Use |
View |
This function is deprecated. |
Unit |
This function is deprecated. |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Boolean |
This function is deprecated. Use |
Unit |
This function is deprecated. |
Unit |
This function is deprecated. |
Unit |
This function is deprecated. |
Unit |
This function is deprecated. |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Unit |
This function is deprecated. Use |
Protected functions |
|
|---|---|
RecyclerView.Adapter |
This function is deprecated. Use |
Inherited Constants |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited functions |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Constants
const val ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT": String!
Fragment argument used to specify the tag of the desired root PreferenceScreen object.
Public constructors
Public functions
funaddPreferencesFromResource(preferencesResId: @XmlRes Int): Unit
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
findPreference
fun <T : Preference?>findPreference(key: CharSequence): T!
Finds a Preference based on its key.
| Parameters | |
|---|---|
key: CharSequence |
The key of the preference to retrieve |
| Returns | |
|---|---|
T! |
The |
| See also | |
|---|---|
findPreference |
fungetPreferenceManager(): PreferenceManager!
Returns the PreferenceManager used by this fragment.
| Returns | |
|---|---|
PreferenceManager! |
The |
fungetPreferenceScreen(): PreferenceScreen!
Gets the root of the preference hierarchy that this fragment is showing.
| Returns | |
|---|---|
PreferenceScreen! |
The |
funonCreateLayoutManager(): RecyclerView.LayoutManager
Called from onCreateRecyclerView to create the RecyclerView.LayoutManager for the created RecyclerView.
| Returns | |
|---|---|
RecyclerView.LayoutManager |
A new |
abstract funonCreatePreferences(savedInstanceState: Bundle?, rootKey: String!): Unit
Called during onCreate to supply the preferences for this fragment. Subclasses are expected to call setPreferenceScreen either directly or via helper methods such as addPreferencesFromResource.
| 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 |
funonCreateRecyclerView(
inflater: LayoutInflater,
parent: ViewGroup,
savedInstanceState: Bundle?
): RecyclerView
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 |
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 layout params 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 |
funonCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View
onDisplayPreferenceDialog
funonDisplayPreferenceDialog(preference: Preference): Unit
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 |
onNavigateToScreen
funonNavigateToScreen(preferenceScreen: PreferenceScreen): Unit
Called by onClick in order to navigate to a new screen of preferences. Calls onPreferenceStartScreen if the target fragment or containing activity implements PreferenceFragment.OnPreferenceStartScreenCallback.
| Parameters | |
|---|---|
preferenceScreen: PreferenceScreen |
The |
onPreferenceTreeClick
funonPreferenceTreeClick(preference: Preference): Boolean
Called when a preference in the tree rooted at this has been clicked.
funsetDivider(divider: Drawable?): Unit
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.
funsetDividerHeight(height: Int): Unit
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.
| Parameters | |
|---|---|
height: Int |
The new height of the divider in pixels |
funsetPreferenceScreen(preferenceScreen: PreferenceScreen!): Unit
Sets the root of the preference hierarchy that this fragment is showing.
| Parameters | |
|---|---|
preferenceScreen: PreferenceScreen! |
The root |
funsetPreferencesFromResource(preferencesResId: @XmlRes Int, key: String?): Unit
Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key.
| Parameters | |
|---|---|
preferencesResId: @XmlRes Int |
The XML resource ID to inflate |
key: String? |
The preference key of the |
Protected functions
protected funonCreateAdapter(preferenceScreen: PreferenceScreen): RecyclerView.Adapter
Creates the root adapter.
| Parameters | |
|---|---|
preferenceScreen: PreferenceScreen |
The |
| Returns | |
|---|---|
RecyclerView.Adapter |
An adapter that contains the preferences contained in this |