PreferenceGroup

abstract class PreferenceGroup : 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(context: Context, attrs: AttributeSet?)
PreferenceGroup(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
PreferenceGroup(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
)

Public functions

Unit

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.

T?

Finds a Preference based on its key.

Int

Gets the maximal number of children that are initially shown.

Preference

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.

Unit
notifyDependencyChange(disableDependents: Boolean)

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

Unit

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

Unit

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

Unit

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.

Unit

Sets the maximal number of children that are shown when the preference group is launched where the rest of the children will be hidden.

Unit
setOrderingAsAdded(orderingAsAdded: Boolean)

Whether to order the Preference children of this group as they are added.

Protected functions

Unit

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

Unit

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.

Unit

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

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

operator Boolean

Returns true if preference is found in this preference group.

inline Unit
PreferenceGroup.forEach(action: (preference: Preference) -> Unit)

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

inline Unit
PreferenceGroup.forEachIndexed(
    action: (index: Int, preference: Preference) -> Unit
)

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

operator Preference

Returns the preference at index.

inline operator T?

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

inline Boolean

Returns true if this preference group contains no preferences.

inline Boolean

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

operator Iterator<Preference>

Returns a MutableIterator over the preferences in this preference group.

inline operator Unit

Removes preference from this preference group.

inline operator Unit

Adds preference to this preference group.

Extension properties

Sequence<Preference>

Returns a Sequence over the preferences in this preference group.

Int

Returns the number of preferences in this preference group.

Inherited Constants

From androidx.preference.Preference
const Int
DEFAULT_ORDER = 2147483647

Specify for setOrder if a specific order is not required.

Inherited functions

From java.lang.Comparable
abstract Int
compareTo(p: T!)
From androidx.preference.Preference
Boolean
callChangeListener(newValue: Any!)

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.

T?

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

Context

Returns the Context of this preference.

String?

Returns the key of the dependency on this preference.

Bundle

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

String?

Return the fragment class name associated with this preference.

Drawable?

Returns the icon of this preference.

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.

Int

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

Preference.OnPreferenceChangeListener?

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

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.

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(defaultReturnValue: Boolean)

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

Float
getPersistedFloat(defaultReturnValue: Float)

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

Int
getPersistedInt(defaultReturnValue: Int)

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

Long
getPersistedLong(defaultReturnValue: Long)

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

String!
getPersistedString(defaultReturnValue: String!)

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

(Mutable)Set<String!>!
getPersistedStringSet(defaultReturnValue: (Mutable)Set<String!>!)

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

PreferenceDataStore?

Returns PreferenceDataStore used by this preference.

PreferenceManager!

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

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.

CharSequence?

Returns the summary of this preference.

Preference.SummaryProvider?

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

CharSequence?

Returns the title of this preference.

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.

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.

Boolean

Checks whether this preference should be visible to the user.

Unit

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

Unit

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

Unit

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

Unit

Binds the created View to the data for this preference.

Unit

Processes a click on the preference.

Unit
onDependencyChanged(dependency: Preference, disableDependent: Boolean)

Called when the dependency changes.

Any?

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

Unit

This function is deprecated.

Preferences aren't views.

Unit
onParentChanged(parent: Preference, disableChild: Boolean)

Called when the implicit parent dependency changes.

Unit

Called when this preference is being removed from the hierarchy.

Unit
onSetInitialValue(defaultValue: Any?)

Implement this to set the initial value of the preference.

Unit
onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any!)

This function is deprecated.

Use onSetInitialValue instead.

Bundle?

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

Boolean

Attempts to persist a Boolean if this preference is persistent.

Boolean

Attempts to persist a Float if this preference is persistent.

Boolean
persistInt(value: Int)

Attempts to persist an Integer if this preference is persistent.

Boolean
persistLong(value: Long)

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.

Unit

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

Unit

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

Unit

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

Unit
setDefaultValue(defaultValue: Any!)

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.

Unit
setDependency(dependencyKey: String?)

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

Unit
setEnabled(enabled: Boolean)

Sets whether this preference is enabled.

Unit
setFragment(fragment: String?)

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

Unit
setIcon(icon: Drawable?)

Sets the icon for this preference with a Drawable.

Unit
setIcon(iconResId: Int)

Sets the icon for this preference with a resource ID.

Unit
setIconSpaceReserved(iconSpaceReserved: Boolean)

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

Unit
setIntent(intent: Intent?)

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

Unit
setKey(key: String!)

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

Unit
setLayoutResource(layoutResId: Int)

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

Unit

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

Unit

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

Unit
setOrder(order: Int)

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

Unit
setPersistent(persistent: Boolean)

Sets whether this preference is persistent.

Unit

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

Unit
setSelectable(selectable: Boolean)

Sets whether this preference is selectable.

Unit
setShouldDisableView(shouldDisableView: Boolean)

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

Unit
setSingleLineTitle(singleLineTitle: Boolean)

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

Unit

Sets the summary for this preference with a CharSequence.

Unit
setSummary(summaryResId: Int)

Sets the summary for this preference with a resource ID.

Unit

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

Unit

Sets the title for this preference with a CharSequence.

Unit
setTitle(titleResId: Int)

Sets the title for this preference with a resource ID.

Unit
setViewId(viewId: Int)

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

Unit
setVisible(visible: Boolean)

Sets whether this preference should be visible to the user.

Unit
setWidgetLayoutResource(widgetLayoutResId: Int)

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.

String

Public constructors

PreferenceGroup

Added in 1.0.0
PreferenceGroup(context: Context, attrs: AttributeSet?)

PreferenceGroup

Added in 1.0.0
PreferenceGroup(context: Context, attrs: AttributeSet?, defStyleAttr: Int)

PreferenceGroup

Added in 1.0.0
PreferenceGroup(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
)

Public functions

addItemFromInflater

Added in 1.0.0
fun addItemFromInflater(preference: Preference): Unit

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

addPreference

Added in 1.0.0
fun addPreference(preference: Preference): Boolean

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

Added in 1.1.0
fun <T : Preference?> findPreference(key: CharSequence): T?

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
key: CharSequence

The key of the Preference to retrieve

Returns
T?

The Preference with the key, or null

getInitialExpandedChildrenCount

Added in 1.0.0
fun getInitialExpandedChildrenCount(): Int

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
fun getPreference(index: Int): Preference

Returns the Preference at a particular index.

Parameters
index: Int

The index of the Preference to retrieve

Returns
Preference

The Preference

getPreferenceCount

Added in 1.0.0
fun getPreferenceCount(): Int

Returns the number of children Preferences.

Returns
Int

The number of preference children in this group

isOrderingAsAdded

Added in 1.0.0
fun isOrderingAsAdded(): Boolean

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

fun notifyDependencyChange(disableDependents: Boolean): Unit

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

Parameters
disableDependents: Boolean

Whether this preference should disable its dependents.

onAttached

fun onAttached(): Unit

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

fun onDetached(): Unit

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
fun removeAll(): Unit

Removes all Preferences from this group.

removePreference

Added in 1.0.0
fun removePreference(preference: Preference): Boolean

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
preference: Preference

The preference to remove

Returns
Boolean

Whether the preference was found and removed

removePreferenceRecursively

Added in 1.1.0
fun removePreferenceRecursively(key: CharSequence): Boolean

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
key: CharSequence

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
fun setInitialExpandedChildrenCount(expandedCount: Int): Unit

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 initialExpandedChildrenCount

setOrderingAsAdded

Added in 1.0.0
fun setOrderingAsAdded(orderingAsAdded: Boolean): Unit

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
setOrder

Protected functions

dispatchRestoreInstanceState

Added in 1.0.0
protected fun dispatchRestoreInstanceState(container: Bundle): Unit

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
container: Bundle

The Bundle that holds the previously saved state

dispatchSaveInstanceState

Added in 1.0.0
protected fun dispatchSaveInstanceState(container: Bundle): Unit

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
container: Bundle

The Bundle in which to save the instance of this preference

isOnSameScreenAsChildren

Added in 1.0.0
protected fun isOnSameScreenAsChildren(): Boolean

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 fun onPrepareAddPreference(preference: Preference): Boolean

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 fun onRestoreInstanceState(state: Parcelable?): Unit

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 fun onSaveInstanceState(): 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. 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.

Extension functions

operator fun PreferenceGroup.contains(preference: Preference): Boolean

Returns true if preference is found in this preference group.

inline fun PreferenceGroup.forEach(action: (preference: Preference) -> Unit): Unit

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

inline fun PreferenceGroup.forEachIndexed(
    action: (index: Int, preference: Preference) -> Unit
): Unit

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

operator fun PreferenceGroup.get(index: Int): Preference

Returns the preference at index.

Throws
kotlin.IndexOutOfBoundsException

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

inline operator fun <T : Preference> PreferenceGroup.get(key: CharSequence): T?

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

inline fun PreferenceGroup.isEmpty(): Boolean

Returns true if this preference group contains no preferences.

inline fun PreferenceGroup.isNotEmpty(): Boolean

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

operator fun PreferenceGroup.iterator(): Iterator<Preference>

Returns a MutableIterator over the preferences in this preference group.

inline operator fun PreferenceGroup.minusAssign(preference: Preference): Unit

Removes preference from this preference group.

inline operator fun PreferenceGroup.plusAssign(preference: Preference): Unit

Adds preference to this preference group.

Extension properties

val PreferenceGroup.childrenSequence<Preference>

Returns a Sequence over the preferences in this preference group.

val PreferenceGroup.sizeInt

Returns the number of preferences in this preference group.