PreferenceGroup


public abstract class PreferenceGroup extends Preference

Known direct subclasses
PreferenceCategory

A container that is used to group similar Preferences.

PreferenceScreen

A top-level container that represents a settings screen.


A container for multiple Preferences. It is a base class for preference objects that are parents, such as PreferenceCategory and PreferenceScreen.

name android:orderingFromXml

name initialExpandedChildrenCount

Summary

Nested types

Interface for PreferenceGroup adapters to implement so that scrollToPreference and scrollToPreference can determine the correct scroll position to request.

Public constructors

PreferenceGroup(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)
PreferenceGroup(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

void

Called by the inflater to add an item to this group.

boolean

Adds a Preference at the correct position based on the preference's order.

@Nullable T

Finds a Preference based on its key.

int

Gets the maximal number of children that are initially shown.

@NonNull Preference
getPreference(int index)

Returns the Preference at a particular index.

int

Returns the number of children Preferences.

boolean

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

Called when the preference hierarchy has been attached to the list of preferences.

void

Called when the preference hierarchy has been detached from the list of preferences.

void

Removes all Preferences from this group.

boolean

Removes a Preference from this group.

boolean

Recursively finds and removes a Preference from this group or a nested group lower down in the hierarchy.

void

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 Preference children of this group as they are added.

Protected methods

void

Called by restoreHierarchyState to retrieve the saved state for this preference and its children.

void

Called by saveHierarchyState to store the instance for this preference and its children.

boolean

Whether this preference group should be shown on the same screen as its contained preferences.

boolean

Prepares a Preference to be added to the group.

void

Hook allowing a preference to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState.

@NonNull Parcelable

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.

Extension functions

final boolean
PreferenceGroupKt.contains(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Returns true if preference is found in this preference group.

final void
PreferenceGroupKt.forEach(
    @NonNull PreferenceGroup receiver,
    @NonNull Function1<@NonNull PreferenceUnit> action
)

Performs the given action on each preference in this preference group.

final void

Performs the given action on each preference in this preference group, providing its sequential index.

final @NonNull Preference
PreferenceGroupKt.get(@NonNull PreferenceGroup receiver, int index)

Returns the preference at index.

final T
<T extends Preference> PreferenceGroupKt.get(
    @NonNull PreferenceGroup receiver,
    @NonNull CharSequence key
)

Returns the preference with key, or null if no preference with key is found.

final @NonNull Sequence<@NonNull Preference>

Returns a Sequence over the preferences in this preference group.

final int

Returns the number of preferences in this preference group.

final boolean

Returns true if this preference group contains no preferences.

final boolean

Returns true if this preference group contains one or more preferences.

final @NonNull Iterator<@NonNull Preference>

Returns a MutableIterator over the preferences in this preference group.

final void
PreferenceGroupKt.minusAssign(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Removes preference from this preference group.

final void
PreferenceGroupKt.plusAssign(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Adds preference to this preference group.

Inherited Constants

From androidx.preference.Preference
static final int
DEFAULT_ORDER = 2147483647

Specify for setOrder if a specific order is not required.

Inherited methods

From java.lang.Comparable
abstract int
compareTo(T p)
From androidx.preference.Preference
boolean

Call this method after the user changes the preference, but before the internal state is set.

int

Compares preference objects based on order (if set), otherwise alphabetically on the titles.

@Nullable T

Finds a preference in the entire hierarchy (above or below this preference) with the given key.

@NonNull Context

Returns the Context of this preference.

@Nullable String

Returns the key of the dependency on this preference.

@NonNull Bundle

Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one.

@Nullable String

Return the fragment class name associated with this preference.

@Nullable Drawable

Returns the icon of this preference.

@Nullable Intent

Return the Intent associated with this preference.

String

Gets the key for this preference, which is also the key used for storing values into SharedPreferences or PreferenceDataStore.

final int

Gets the layout resource that will be shown as the View for this preference.

@Nullable Preference.OnPreferenceChangeListener

Returns the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).

@Nullable Preference.OnPreferenceClickListener

Returns the callback to be invoked when this preference is clicked.

int

Gets the order of this preference with respect to other preference objects on the same level.

@Nullable PreferenceGroup

Returns the PreferenceGroup which is this preference assigned to or null if this preference is not assigned to any group or is a root preference.

boolean
getPersistedBoolean(boolean defaultReturnValue)

Attempts to get a persisted Boolean if this preference is persistent.

float
getPersistedFloat(float defaultReturnValue)

Attempts to get a persisted Float if this preference is persistent.

int
getPersistedInt(int defaultReturnValue)

Attempts to get a persisted Integer if this preference is persistent.

long
getPersistedLong(long defaultReturnValue)

Attempts to get a persisted Long if this preference is persistent.

String
getPersistedString(String defaultReturnValue)

Attempts to get a persisted set of Strings if this preference is persistent.

Set<String>
getPersistedStringSet(Set<String> defaultReturnValue)

Attempts to get a persisted set of Strings if this preference is persistent.

@Nullable PreferenceDataStore

Returns PreferenceDataStore used by this preference.

PreferenceManager

Gets the PreferenceManager that manages this preference object's tree.

@Nullable SharedPreferences

Returns the SharedPreferences where this preference can read its value(s).

boolean

Checks whether this preference should disable its view when it's action is disabled.

@Nullable CharSequence

Returns the summary of this preference.

final @Nullable Preference.SummaryProvider

Returns the SummaryProvider used to configure the summary of this preference.

@Nullable CharSequence

Returns the title of this preference.

final int

Gets the layout resource for the controllable widget portion of this preference.

boolean

Checks whether this preference has a valid key.

boolean

Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference.

boolean

Checks whether this preference should be enabled in the list.

boolean

Returns whether the space of this preference icon view is reserved.

boolean

Checks whether this preference is persistent.

boolean

Checks whether this preference should be selectable in the list.

final boolean

Checks whether this preference is shown to the user in the hierarchy.

boolean

Gets whether the title of this preference is constrained to a single line.

final boolean

Checks whether this preference should be visible to the user.

void

Should be called when the data of this Preference has changed.

void

Should be called when a preference has been added/removed from this group, or the ordering should be re-evaluated.

void

Called when this preference has been attached to a preference hierarchy.

void

Binds the created View to the data for this preference.

void

Processes a click on the preference.

void
onDependencyChanged(
    @NonNull Preference dependency,
    boolean disableDependent
)

Called when the dependency changes.

@Nullable Object

Called when a preference is being inflated and the default value attribute needs to be read.

void

This method is deprecated.

Preferences aren't views.

void
onParentChanged(@NonNull Preference parent, boolean disableChild)

Called when the implicit parent dependency changes.

void

Called when this preference is being removed from the hierarchy.

void

Implement this to set the initial value of the preference.

void
onSetInitialValue(boolean restorePersistedValue, Object defaultValue)

This method is deprecated.

Use onSetInitialValue instead.

@Nullable Bundle

Return the extras Bundle object associated with this preference, returning null if there is not currently one.

boolean
persistBoolean(boolean value)

Attempts to persist a Boolean if this preference is persistent.

boolean
persistFloat(float value)

Attempts to persist a Float if this preference is persistent.

boolean
persistInt(int value)

Attempts to persist an Integer if this preference is persistent.

boolean
persistLong(long value)

Attempts to persist a Long if this preference is persistent.

boolean

Attempts to persist a String if this preference is persistent.

boolean

Attempts to persist a set of Strings if this preference is persistent.

void

Restore this preference hierarchy's previously saved state from the given container.

void

Store this preference hierarchy's frozen state into the given container.

void
setCopyingEnabled(boolean enabled)

Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference.

void
setDefaultValue(Object defaultValue)

Sets the default value for this preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage.

void
setDependency(@Nullable String dependencyKey)

Sets the key of a preference that this preference will depend on.

void
setEnabled(boolean enabled)

Sets whether this preference is enabled.

void

Sets the class name of a fragment to be shown when this preference is clicked.

void

Sets the icon for this preference with a Drawable.

void
setIcon(int iconResId)

Sets the icon for this preference with a resource ID.

void
setIconSpaceReserved(boolean iconSpaceReserved)

Sets whether to reserve the space of this preference icon view when no icon is provided.

void

Sets an Intent to be used for startActivity when this preference is clicked.

void

Sets the key for this preference, which is used as a key to the SharedPreferences or PreferenceDataStore.

void
setLayoutResource(int layoutResId)

Sets the layout resource that is inflated as the View to be shown for this preference.

void

Sets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).

void

Sets the callback to be invoked when this preference is clicked.

void
setOrder(int order)

Sets the order of this preference with respect to other preference objects on the same level.

void
setPersistent(boolean persistent)

Sets whether this preference is persistent.

void

Sets a PreferenceDataStore to be used by this preference instead of using SharedPreferences.

void
setSelectable(boolean selectable)

Sets whether this preference is selectable.

void
setShouldDisableView(boolean shouldDisableView)

Sets whether this preference should disable its view when it gets disabled.

void
setSingleLineTitle(boolean singleLineTitle)

Sets whether to constrain the title of this preference to a single line instead of letting it wrap onto multiple lines.

void

Sets the summary for this preference with a CharSequence.

void
setSummary(int summaryResId)

Sets the summary for this preference with a resource ID.

final void

Set a SummaryProvider that will be invoked whenever the summary of this preference is requested.

void

Sets the title for this preference with a CharSequence.

void
setTitle(int titleResId)

Sets the title for this preference with a resource ID.

void
setViewId(int viewId)

Set the ID that will be assigned to the overall View representing this preference, once bound.

final void
setVisible(boolean visible)

Sets whether this preference should be visible to the user.

void
setWidgetLayoutResource(int widgetLayoutResId)

Sets the layout for the controllable widget portion of this preference.

boolean

Checks whether this preference's dependents should currently be disabled.

boolean

Checks whether, at the given time this method is called, this preference should store/restore its value(s) into the SharedPreferences or into PreferenceDataStore if assigned.

@NonNull String

Public constructors

PreferenceGroup

Added in 1.0.0
public PreferenceGroup(@NonNull Context context, @Nullable AttributeSet attrs)

PreferenceGroup

Added in 1.0.0
public PreferenceGroup(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

PreferenceGroup

Added in 1.0.0
public PreferenceGroup(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

addItemFromInflater

Added in 1.0.0
public void addItemFromInflater(@NonNull Preference preference)

Called by the inflater to add an item to this group.

addPreference

Added in 1.0.0
public boolean addPreference(@NonNull Preference preference)

Adds a Preference at the correct position based on the preference's order.

Parameters
@NonNull Preference preference

The preference to add

Returns
boolean

Whether the preference is now in this group

findPreference

Added in 1.1.0
public @Nullable T <T extends Preference> findPreference(@NonNull CharSequence key)

Finds a Preference based on its key. If two Preferences 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 PreferenceGroups.

Parameters
@NonNull CharSequence key

The key of the Preference to retrieve

Returns
@Nullable T

The Preference with the key, or null

getInitialExpandedChildrenCount

Added in 1.0.0
public int getInitialExpandedChildrenCount()

Gets the maximal number of children that are initially shown.

Returns
int

The maximal number of children that are initially shown initialExpandedChildrenCount

getPreference

Added in 1.0.0
public @NonNull Preference getPreference(int index)

Returns the Preference at a particular index.

Parameters
int index

The index of the Preference to retrieve

getPreferenceCount

Added in 1.0.0
public int getPreferenceCount()

Returns the number of children Preferences.

Returns
int

The number of preference children in this group

isOrderingAsAdded

Added in 1.0.0
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

notifyDependencyChange

public void notifyDependencyChange(boolean disableDependents)

Notifies any listening dependents of a change that affects the dependency.

Parameters
boolean disableDependents

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.

removeAll

Added in 1.0.0
public void removeAll()

Removes all Preferences from this group.

removePreference

Added in 1.0.0
public boolean removePreference(@NonNull 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 to recursively find and remove a preference.

Parameters
@NonNull Preference preference

The preference to remove

Returns
boolean

Whether the preference was found and removed

removePreferenceRecursively

Added in 1.1.0
public boolean removePreferenceRecursively(@NonNull CharSequence key)

Recursively finds and removes a Preference from this group or a nested group lower down in the hierarchy. If two Preferences share the same key (not recommended), the first to appear will be removed.

Parameters
@NonNull CharSequence key

The key of the preference to remove

Returns
boolean

Whether the preference was found and removed

See also
findPreference

setInitialExpandedChildrenCount

Added in 1.0.0
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
int expandedCount

The number of children that is initially shown initialExpandedChildrenCount

setOrderingAsAdded

Added in 1.0.0
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
boolean orderingAsAdded

Whether to order according to the order added

See also
setOrder

Protected methods

dispatchRestoreInstanceState

Added in 1.0.0
protected void dispatchRestoreInstanceState(@NonNull Bundle container)

Called by restoreHierarchyState 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
@NonNull Bundle container

The Bundle that holds the previously saved state

dispatchSaveInstanceState

Added in 1.0.0
protected void dispatchSaveInstanceState(@NonNull Bundle container)

Called by saveHierarchyState 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
@NonNull Bundle container

The Bundle in which to save the instance of this preference

isOnSameScreenAsChildren

Added in 1.0.0
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

Added in 1.0.0
protected boolean onPrepareAddPreference(@NonNull Preference preference)

Prepares a Preference to be added to the group.

Parameters
@NonNull Preference preference

The preference to add

Returns
boolean

Whether to allow adding the preference (true), or not (false)

onRestoreInstanceState

protected void onRestoreInstanceState(@Nullable 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
@Nullable Parcelable state

The saved state that had previously been returned by onSaveInstanceState.

onSaveInstanceState

protected @NonNull 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
@NonNull 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.

Extension functions

PreferenceGroupKt.contains

public final boolean PreferenceGroupKt.contains(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Returns true if preference is found in this preference group.

PreferenceGroupKt.forEach

public final void PreferenceGroupKt.forEach(
    @NonNull PreferenceGroup receiver,
    @NonNull Function1<@NonNull PreferenceUnit> action
)

Performs the given action on each preference in this preference group.

PreferenceGroupKt.forEachIndexed

public final void PreferenceGroupKt.forEachIndexed(
    @NonNull PreferenceGroup receiver,
    @NonNull Function2<@NonNull Integer, @NonNull PreferenceUnit> action
)

Performs the given action on each preference in this preference group, providing its sequential index.

PreferenceGroupKt.get

public final @NonNull Preference PreferenceGroupKt.get(@NonNull PreferenceGroup receiver, int index)

Returns the preference at index.

Throws
kotlin.IndexOutOfBoundsException

if index is less than 0 or greater than or equal to the count.

PreferenceGroupKt.get

public final T <T extends Preference> PreferenceGroupKt.get(
    @NonNull PreferenceGroup receiver,
    @NonNull CharSequence key
)

Returns the preference with key, or null if no preference with key is found.

PreferenceGroupKt.getChildren

public final @NonNull Sequence<@NonNull PreferencePreferenceGroupKt.getChildren(@NonNull PreferenceGroup receiver)

Returns a Sequence over the preferences in this preference group.

PreferenceGroupKt.getSize

public final int PreferenceGroupKt.getSize(@NonNull PreferenceGroup receiver)

Returns the number of preferences in this preference group.

PreferenceGroupKt.isEmpty

public final boolean PreferenceGroupKt.isEmpty(@NonNull PreferenceGroup receiver)

Returns true if this preference group contains no preferences.

PreferenceGroupKt.isNotEmpty

public final boolean PreferenceGroupKt.isNotEmpty(@NonNull PreferenceGroup receiver)

Returns true if this preference group contains one or more preferences.

PreferenceGroupKt.iterator

public final @NonNull Iterator<@NonNull PreferencePreferenceGroupKt.iterator(@NonNull PreferenceGroup receiver)

Returns a MutableIterator over the preferences in this preference group.

PreferenceGroupKt.minusAssign

public final void PreferenceGroupKt.minusAssign(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Removes preference from this preference group.

PreferenceGroupKt.plusAssign

public final void PreferenceGroupKt.plusAssign(
    @NonNull PreferenceGroup receiver,
    @NonNull Preference preference
)

Adds preference to this preference group.