PreferenceFragment
public abstract class PreferenceFragment extends Fragment implements 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 |
|---|
public interface PreferenceFragment.OnPreferenceDisplayDialogCallbackInterface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. |
public interface PreferenceFragment.OnPreferenceStartFragmentCallbackInterface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. |
public interface PreferenceFragment.OnPreferenceStartScreenCallbackInterface 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 final String |
ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"This field is deprecated. Use |
Public constructors |
|---|
Public methods |
|
|---|---|
void |
This method is deprecated. Use |
T |
<T extends Preference> This method is deprecated. Use |
final RecyclerView |
This method is deprecated. Use |
PreferenceManager |
This method is deprecated. Use |
PreferenceScreen |
This method is deprecated. Use |
void |
This method is deprecated. |
@NonNull RecyclerView.LayoutManager |
This method is deprecated. Use |
abstract void |
This method is deprecated. Use |
@NonNull RecyclerView |
This method is deprecated. Use |
@NonNull View |
This method is deprecated. |
void |
This method is deprecated. |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
boolean |
This method is deprecated. Use |
void |
This method is deprecated. |
void |
This method is deprecated. |
void |
This method is deprecated. |
void |
This method is deprecated. |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
void |
This method is deprecated. Use |
Protected methods |
|
|---|---|
@NonNull RecyclerView.Adapter |
This method is deprecated. Use |
Inherited Constants |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Constants
public static final String ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"
Fragment argument used to specify the tag of the desired root PreferenceScreen object.
Public constructors
Public methods
public voidaddPreferencesFromResource(@XmlRes int preferencesResId)
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
| Parameters | |
|---|---|
@XmlRes int preferencesResId |
The XML resource ID to inflate |
findPreference
public T <T extends Preference>findPreference(@NonNull CharSequence key)
Finds a Preference based on its key.
| Parameters | |
|---|---|
@NonNull CharSequence key |
The key of the preference to retrieve |
| Returns | |
|---|---|
T |
The |
| See also | |
|---|---|
findPreference |
public PreferenceManagergetPreferenceManager()
Returns the PreferenceManager used by this fragment.
| Returns | |
|---|---|
PreferenceManager |
The |
public PreferenceScreengetPreferenceScreen()
Gets the root of the preference hierarchy that this fragment is showing.
| Returns | |
|---|---|
PreferenceScreen |
The |
public @NonNull RecyclerView.LayoutManageronCreateLayoutManager()
Called from onCreateRecyclerView to create the RecyclerView.LayoutManager for the created RecyclerView.
| Returns | |
|---|---|
@NonNull RecyclerView.LayoutManager |
A new |
public abstract voidonCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey)
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 | |
|---|---|
@Nullable Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |
String rootKey |
If non-null, this preference fragment should be rooted at the |
public @NonNull RecyclerViewonCreateRecyclerView(
@NonNull LayoutInflater inflater,
@NonNull ViewGroup parent,
@Nullable Bundle savedInstanceState
)
Creates the RecyclerView used to display the preferences. Subclasses may override this to return a customized RecyclerView.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The LayoutInflater object that can be used to inflate the |
@NonNull ViewGroup parent |
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. |
@Nullable Bundle savedInstanceState |
If non-null, this view is being re-constructed from a previous saved state as given here. |
| Returns | |
|---|---|
@NonNull RecyclerView |
A new |
public @NonNull ViewonCreateView(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState
)
onDisplayPreferenceDialog
public voidonDisplayPreferenceDialog(@NonNull 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 | |
|---|---|
@NonNull Preference preference |
The |
onNavigateToScreen
public voidonNavigateToScreen(@NonNull PreferenceScreen preferenceScreen)
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 | |
|---|---|
@NonNull PreferenceScreen preferenceScreen |
The |
onPreferenceTreeClick
public booleanonPreferenceTreeClick(@NonNull Preference preference)
Called when a preference in the tree rooted at this has been clicked.
public voidsetDivider(@Nullable 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.
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.
| Parameters | |
|---|---|
int height |
The new height of the divider in pixels |
public voidsetPreferenceScreen(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing.
| Parameters | |
|---|---|
PreferenceScreen preferenceScreen |
The root |
public voidsetPreferencesFromResource(
@XmlRes int preferencesResId,
@Nullable String key
)
Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key.
| Parameters | |
|---|---|
@XmlRes int preferencesResId |
The XML resource ID to inflate |
@Nullable String key |
The preference key of the |
Protected methods
protected @NonNull RecyclerView.AdapteronCreateAdapter(@NonNull PreferenceScreen preferenceScreen)
Creates the root adapter.
| Parameters | |
|---|---|
@NonNull PreferenceScreen preferenceScreen |
The |
| Returns | |
|---|---|
@NonNull RecyclerView.Adapter |
An adapter that contains the preferences contained in this |