PreferenceGroup
public
abstract
class
PreferenceGroup
extends Preference
java.lang.Object | ||
↳ | androidx.preference.Preference | |
↳ | androidx.preference.PreferenceGroup |
A container for multiple Preference
s. It is a base class for preference
objects that are parents, such as PreferenceCategory
and PreferenceScreen
.
Developer Guides
For information about building a settings screen using the AndroidX Preference library, see Settings.
Summary
Nested classes | |
---|---|
interface |
PreferenceGroup.PreferencePositionCallback
Interface for PreferenceGroup adapters to implement so that
|
Inherited constants |
---|
Public constructors | |
---|---|
PreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
|
PreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr)
|
|
PreferenceGroup(Context context, AttributeSet attrs)
|
Public methods | |
---|---|
void
|
addItemFromInflater(Preference preference)
Called by the inflater to add an item to this group. |
boolean
|
addPreference(Preference preference)
Adds a |
<T extends Preference>
T
|
findPreference(CharSequence key)
Finds a |
int
|
getInitialExpandedChildrenCount()
Gets the maximal number of children that are initially shown. |
Preference
|
getPreference(int index)
Returns the |
int
|
getPreferenceCount()
Returns the number of children |
boolean
|
isOrderingAsAdded()
Whether this group is ordering preferences in the order they are added. |
void
|
notifyDependencyChange(boolean disableDependents)
Notifies any listening dependents of a change that affects the dependency. |
void
|
onAttached()
Called when the preference hierarchy has been attached to the list of preferences. |
void
|
onDetached()
Called when the preference hierarchy has been detached from the list of preferences. |
void
|
removeAll()
Removes all |
boolean
|
removePreference(Preference preference)
Removes a |
boolean
|
removePreferenceRecursively(CharSequence key)
Recursively finds and removes a |
void
|
setInitialExpandedChildrenCount(int expandedCount)
Sets the maximal number of children that are shown when the preference group is launched where the rest of the children will be hidden. |
void
|
setOrderingAsAdded(boolean orderingAsAdded)
Whether to order the |
Protected methods | |
---|---|
void
|
dispatchRestoreInstanceState(Bundle container)
Called by |
void
|
dispatchSaveInstanceState(Bundle container)
Called by |
boolean
|
isOnSameScreenAsChildren()
Whether this preference group should be shown on the same screen as its contained preferences. |
boolean
|
onPrepareAddPreference(Preference preference)
Prepares a |
void
|
onRestoreInstanceState(Parcelable state)
Hook allowing a preference to re-apply a representation of its internal state that had
previously been generated by |
Parcelable
|
onSaveInstanceState()
Hook allowing a preference to generate a representation of its internal state that can later be used to create a new instance with that same state. |
Inherited methods | |
---|---|
Public constructors
PreferenceGroup
public PreferenceGroup (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
PreferenceGroup
public PreferenceGroup (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
PreferenceGroup
public PreferenceGroup (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
Public methods
addItemFromInflater
public void addItemFromInflater (Preference preference)
Called by the inflater to add an item to this group.
Parameters | |
---|---|
preference |
Preference |
addPreference
public boolean addPreference (Preference preference)
Adds a Preference
at the correct position based on the preference's order.
Parameters | |
---|---|
preference |
Preference : The preference to add |
Returns | |
---|---|
boolean |
Whether the preference is now in this group |
findPreference
public T findPreference (CharSequence key)
Finds a Preference
based on its key. If two Preference
s share the same key
(not recommended), the first to appear will be returned.
This will recursively search for the Preference
in any children that are also
PreferenceGroup
s.
Parameters | |
---|---|
key |
CharSequence : The key of the Preference to retrieve |
Returns | |
---|---|
T |
The Preference with the key, or null
|
getInitialExpandedChildrenCount
public int getInitialExpandedChildrenCount ()
Gets the maximal number of children that are initially shown.
Returns | |
---|---|
int |
The maximal number of children that are initially shown
R.attr.initialExpandedChildrenCount
|
getPreference
public Preference getPreference (int index)
Returns the Preference
at a particular index.
Parameters | |
---|---|
index |
int : The index of the Preference to retrieve |
Returns | |
---|---|
Preference |
The Preference
|
getPreferenceCount
public int getPreferenceCount ()
Returns the number of children Preference
s.
Returns | |
---|---|
int |
The number of preference children in this group |
isOrderingAsAdded
public boolean isOrderingAsAdded ()
Whether this group is ordering preferences in the order they are added.
Returns | |
---|---|
boolean |
Whether this group orders based on the order the children are added |
See also:
notifyDependencyChange
public void notifyDependencyChange (boolean disableDependents)
Notifies any listening dependents of a change that affects the dependency.
Parameters | |
---|---|
disableDependents |
boolean : Whether this preference should disable
its dependents.
|
onAttached
public void onAttached ()
Called when the preference hierarchy has been attached to the list of preferences. This can also be called when this preference has been attached to a group that was already attached to the list of preferences.
onDetached
public void onDetached ()
Called when the preference hierarchy has been detached from the list of preferences. This can also be called when this preference has been removed from a group that was attached to the list of preferences.
removePreference
public boolean removePreference (Preference preference)
Removes a Preference
from this group.
Note: This action is not recursive, and will only remove a preference if it exists in
this group, ignoring preferences found in nested groups. Use
removePreferenceRecursively(CharSequence)
to recursively find and remove a
preference.
Parameters | |
---|---|
preference |
Preference : The preference to remove |
Returns | |
---|---|
boolean |
Whether the preference was found and removed |
removePreferenceRecursively
public boolean removePreferenceRecursively (CharSequence key)
Recursively finds and removes a Preference
from this group or a nested group lower
down in the hierarchy. If two Preference
s share the same key (not recommended),
the first to appear will be removed.
Parameters | |
---|---|
key |
CharSequence : The key of the preference to remove |
Returns | |
---|---|
boolean |
Whether the preference was found and removed |
See also:
setInitialExpandedChildrenCount
public void setInitialExpandedChildrenCount (int expandedCount)
Sets the maximal number of children that are shown when the preference group is launched where the rest of the children will be hidden. If some children are hidden an expand button will be provided to show all the hidden children. Any child in any level of the hierarchy that is also a preference group (e.g. preference category) will not be counted towards the limit. But instead the children of such group will be counted. By default, all children will be shown, so the default value of this attribute is equal to Integer.MAX_VALUE.
Note: The group should have a key defined if an expandable preference is present to correctly persist state.
Parameters | |
---|---|
expandedCount |
int : The number of children that is initially shown
R.attr.initialExpandedChildrenCount
|
setOrderingAsAdded
public void setOrderingAsAdded (boolean orderingAsAdded)
Whether to order the Preference
children of this group as they are added. If this
is false, the ordering will follow each Preference order and default to alphabetic for
those without an order.
If this is called after preferences are added, they will not be re-ordered in the order they were added, hence call this method early on.
Parameters | |
---|---|
orderingAsAdded |
boolean : Whether to order according to the order added |
See also:
Protected methods
dispatchRestoreInstanceState
protected void dispatchRestoreInstanceState (Bundle container)
Called by restoreHierarchyState(Bundle)
to retrieve the saved state for this preference
and its children. May be overridden to modify how restoring happens to the children of a
preference. For example, some preference objects may not want to save state for their
children.
Parameters | |
---|---|
container |
Bundle : The Bundle that holds the previously saved state |
dispatchSaveInstanceState
protected void dispatchSaveInstanceState (Bundle container)
Called by saveHierarchyState(Bundle)
to store the instance for this preference and its
children. May be overridden to modify how the save happens for children. For example, some
preference objects may want to not store an instance for their children.
Parameters | |
---|---|
container |
Bundle : The Bundle in which to save the instance of this preference |
isOnSameScreenAsChildren
protected boolean isOnSameScreenAsChildren ()
Whether this preference group should be shown on the same screen as its contained preferences.
Returns | |
---|---|
boolean |
true if the contained preferences should be shown on the same screen as this
preference.
|
onPrepareAddPreference
protected boolean onPrepareAddPreference (Preference preference)
Prepares a Preference
to be added to the group.
Parameters | |
---|---|
preference |
Preference : The preference to add |
Returns | |
---|---|
boolean |
Whether to allow adding the preference (true ), or not (false )
|
onRestoreInstanceState
protected void onRestoreInstanceState (Parcelable state)
Hook allowing a preference to re-apply a representation of its internal state that had
previously been generated by onSaveInstanceState()
. This function will never be
called with a null state.
Parameters | |
---|---|
state |
Parcelable : The saved state that had previously been returned by
onSaveInstanceState() . |
onSaveInstanceState
protected Parcelable onSaveInstanceState ()
Hook allowing a preference to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can be reconstructed later.
Returns | |
---|---|
Parcelable |
A Parcelable object containing the current dynamic state of this preference, or
null if there is nothing interesting to save. The default implementation returns
null . |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-09-30 UTC.