Preference
open class Preference : Comparable<Preference!>
kotlin.Any | |
↳ | androidx.preference.Preference |
The basic building block that represents an individual setting displayed to a user in the preference hierarchy. This class provides the data that will be displayed to the user and has a reference to the SharedPreferences
or PreferenceDataStore
instance that persists the preference's values.
When specifying a preference hierarchy in XML, each element can point to a subclass of Preference
, similar to the view hierarchy and layouts.
This class contains a key
that that represents the key that is used to persist the value to the device. It is up to the subclass to decide how to store the value.
Summary
Nested classes |
|
---|---|
open |
A base class for managing the instance state of a |
abstract |
Interface definition for a callback to be invoked when the value of this |
abstract |
Interface definition for a callback to be invoked when a |
abstract |
Interface definition for a callback to be invoked when the summary of this |
Constants |
|
---|---|
static Int |
Specify for |
Public constructors |
|
---|---|
<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int) Perform inflation from XML and apply a class-specific base style. |
|
<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int) Perform inflation from XML and apply a class-specific base style. |
|
<init>(context: Context!, attrs: AttributeSet!) Constructor that is called when inflating a preference from XML. |
|
Constructor to create a preference. |
Public methods |
|
---|---|
open Boolean |
callChangeListener(newValue: Any!) Call this method after the user changes the preference, but before the internal state is set. |
open Int |
compareTo(@NonNull other: Preference) Compares preference objects based on order (if set), otherwise alphabetically on the titles. |
open Context! |
Returns the |
open String! |
Returns the key of the dependency on this preference. |
open Bundle! |
Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one. |
open String! |
Return the fragment class name associated with this preference. |
open Drawable! |
getIcon() Returns the icon of this preference. |
open Intent! |
Return the |
open String! |
getKey() Gets the key for this preference, which is also the key used for storing values into |
Int |
Gets the layout resource that will be shown as the |
open Preference.OnPreferenceChangeListener! |
Returns the callback to be invoked when this preference is changed by the user (but before the internal state has been updated). |
open Preference.OnPreferenceClickListener! |
Returns the callback to be invoked when this preference is clicked. |
open Int |
getOrder() Gets the order of this preference with respect to other preference objects on the same level. |
open PreferenceGroup? |
Returns the |
open MutableSet<String!>! |
getPersistedStringSet(defaultReturnValue: MutableSet<String!>!) Attempts to get a persisted set of Strings if this preference is persistent. |
open PreferenceDataStore? |
Returns |
open PreferenceManager! |
Gets the |
open SharedPreferences! |
Returns the |
open Boolean |
Checks whether this preference should disable its view when it's action is disabled. |
open CharSequence! |
Returns the summary of this preference. |
Preference.SummaryProvider<Preference!>? |
Returns the |
open CharSequence! |
getTitle() Returns the title of this preference. |
Int |
Gets the layout resource for the controllable widget portion of this preference. |
open Boolean |
hasKey() Checks whether this preference has a valid key. |
open Boolean |
Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference. |
open Boolean |
Checks whether this preference should be enabled in the list. |
open Boolean |
Returns whether the space of this preference icon view is reserved. |
open Boolean |
Checks whether this preference is persistent. |
open Boolean |
Checks whether this preference should be selectable in the list. |
Boolean |
isShown() Checks whether this preference is shown to the user in the hierarchy. |
open 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. |
open Unit |
notifyDependencyChange(disableDependents: Boolean) Notifies any listening dependents of a change that affects the dependency. |
open Unit |
Called when the preference hierarchy has been attached to the list of preferences. |
open Unit |
onBindViewHolder(holder: PreferenceViewHolder!) Binds the created View to the data for this preference. |
open Unit |
onDependencyChanged(dependency: Preference!, disableDependent: Boolean) Called when the dependency changes. |
open Unit |
Called when the preference hierarchy has been detached from the list of preferences. |
open Unit |
Initializes an |
open Unit |
onParentChanged(parent: Preference!, disableChild: Boolean) Called when the implicit parent dependency changes. |
open Bundle! |
Return the extras Bundle object associated with this preference, returning |
open Boolean |
persistStringSet(values: MutableSet<String!>!) Attempts to persist a set of Strings if this preference is persistent. |
open Unit |
restoreHierarchyState(container: Bundle!) Restore this preference hierarchy's previously saved state from the given container. |
open Unit |
saveHierarchyState(container: Bundle!) Store this preference hierarchy's frozen state into the given container. |
open Unit |
setCopyingEnabled(enabled: Boolean) Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference. |
open 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. |
open Unit |
setDependency(dependencyKey: String!) Sets the key of a preference that this preference will depend on. |
open Unit |
setEnabled(enabled: Boolean) Sets whether this preference is enabled. |
open Unit |
setFragment(fragment: String!) Sets the class name of a fragment to be shown when this preference is clicked. |
open Unit |
Sets the icon for this preference with a Drawable. |
open Unit |
Sets the icon for this preference with a resource ID. |
open Unit |
setIconSpaceReserved(iconSpaceReserved: Boolean) Sets whether to reserve the space of this preference icon view when no icon is provided. |
open Unit |
Sets an |
open Unit |
Sets the key for this preference, which is used as a key to the |
open Unit |
setLayoutResource(layoutResId: Int) Sets the layout resource that is inflated as the |
open Unit |
setOnPreferenceChangeListener(onPreferenceChangeListener: Preference.OnPreferenceChangeListener!) Sets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated). |
open Unit |
setOnPreferenceClickListener(onPreferenceClickListener: Preference.OnPreferenceClickListener!) Sets the callback to be invoked when this preference is clicked. |
open Unit |
Sets the order of this preference with respect to other preference objects on the same level. |
open Unit |
setPersistent(persistent: Boolean) Sets whether this preference is persistent. |
open Unit |
setPreferenceDataStore(dataStore: PreferenceDataStore!) Sets a |
open Unit |
setSelectable(selectable: Boolean) Sets whether this preference is selectable. |
open Unit |
setShouldDisableView(shouldDisableView: Boolean) Sets whether this preference should disable its view when it gets disabled. |
open 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. |
open Unit |
setSummary(summary: CharSequence!) Sets the summary for this preference with a CharSequence. |
open Unit |
setSummary(summaryResId: Int) Sets the summary for this preference with a resource ID. |
Unit |
setSummaryProvider(@Nullable summaryProvider: Preference.SummaryProvider<Preference!>?) Set a |
open Unit |
setTitle(title: CharSequence!) Sets the title for this preference with a CharSequence. |
open Unit |
Sets the title for this preference with a resource ID. |
open Unit |
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. |
open Unit |
setWidgetLayoutResource(widgetLayoutResId: Int) Sets the layout for the controllable widget portion of this preference. |
open Boolean |
Checks whether this preference's dependents should currently be disabled. |
open String |
toString() |
Protected methods |
|
---|---|
open T? |
findPreferenceInHierarchy(@NonNull key: String) Finds a preference in the entire hierarchy (above or below this preference) with the given key. |
open Boolean |
getPersistedBoolean(defaultReturnValue: Boolean) Attempts to get a persisted |
open Float |
getPersistedFloat(defaultReturnValue: Float) Attempts to get a persisted |
open Int |
getPersistedInt(defaultReturnValue: Int) Attempts to get a persisted |
open Long |
getPersistedLong(defaultReturnValue: Long) Attempts to get a persisted |
open String! |
getPersistedString(defaultReturnValue: String!) Attempts to get a persisted set of Strings if this preference is persistent. |
open Unit |
Should be called when the data of this |
open Unit |
Should be called when a preference has been added/removed from this group, or the ordering should be re-evaluated. |
open Unit |
onAttachedToHierarchy(preferenceManager: PreferenceManager!) Called when this preference has been attached to a preference hierarchy. |
open Unit |
onClick() Processes a click on the preference. |
open Any! |
onGetDefaultValue(a: TypedArray!, index: Int) Called when a preference is being inflated and the default value attribute needs to be read. |
open Unit |
Called when this preference is being removed from the hierarchy. |
open Unit |
onRestoreInstanceState(state: Parcelable!) Hook allowing a preference to re-apply a representation of its internal state that had previously been generated by |
open 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. |
open Unit |
onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any!) Implement this to set the initial value of the preference. |
open Unit |
onSetInitialValue(@Nullable defaultValue: Any?) Implement this to set the initial value of the preference. |
open Boolean |
persistBoolean(value: Boolean) Attempts to persist a |
open Boolean |
persistFloat(value: Float) Attempts to persist a |
open Boolean |
persistInt(value: Int) Attempts to persist an |
open Boolean |
persistLong(value: Long) Attempts to persist a |
open Boolean |
persistString(value: String!) Attempts to persist a |
open Boolean |
Checks whether, at the given time this method is called, this preference should store/restore its value(s) into the |
Constants
DEFAULT_ORDER
static val DEFAULT_ORDER: Int
Specify for setOrder(int)
if a specific order is not required.
Value: Integer.MAX_VALUE
Public constructors
<init>
Preference(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)
Perform inflation from XML and apply a class-specific base style. This constructor allows subclasses to use their own base style when they are inflating. For example, a CheckBoxPreference
constructor calls this version of the super class constructor and supplies android.R.attr.checkBoxPreferenceStyle
for defStyleAttr. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as the CheckBoxPreference
class's attributes.
Parameters | |
---|---|
context |
Context!: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences , etc. |
attrs |
Context!: The attributes of the XML tag that is inflating the preference |
defStyleAttr |
Context!: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
defStyleRes |
Context!: A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
<init>
Preference(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)
Perform inflation from XML and apply a class-specific base style. This constructor allows subclasses to use their own base style when they are inflating. For example, a CheckBoxPreference
constructor calls this version of the super class constructor and supplies android.R.attr.checkBoxPreferenceStyle
for defStyleAttr. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as the CheckBoxPreference
class's attributes.
Parameters | |
---|---|
context |
Context!: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences , etc. |
attrs |
Context!: The attributes of the XML tag that is inflating the preference |
defStyleAttr |
Context!: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
See Also
<init>
Preference(context: Context!, attrs: AttributeSet!)
Constructor that is called when inflating a preference from XML. This is called when a preference is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.
Parameters | |
---|---|
context |
Context!: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences , etc. |
attrs |
Context!: The attributes of the XML tag that is inflating the preference |
<init>
Preference(context: Context!)
Constructor to create a preference.
Parameters | |
---|---|
context |
Context!: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences , etc. |
Public methods
callChangeListener
open fun callChangeListener(newValue: Any!): Boolean
Call this method after the user changes the preference, but before the internal state is set. This allows the client to ignore the user value.
Parameters | |
---|---|
newValue |
Any!: The new value of this preference |
Return | |
---|---|
Boolean: true if the user value should be set as the preference value (and persisted) |
compareTo
open fun compareTo(@NonNull other: Preference): Int
Compares preference objects based on order (if set), otherwise alphabetically on the titles.
Parameters | |
---|---|
another |
Preference: The preference to compare to this one |
Return | |
---|---|
Int: 0 if the same; less than 0 if this preference sorts ahead of another; greater than 0 if this preference sorts after another. |
getContext
open fun getContext(): Context!
Returns the Context
of this preference. Each preference in a preference hierarchy can be from different Context (for example, if multiple activities provide preferences into a single PreferenceFragmentCompat
). This Context will be used to save the preference values.
Return | |
---|---|
Context!: The Context of this preference |
getDependency
open fun getDependency(): String!
Returns the key of the dependency on this preference.
Return | |
---|---|
String!: The key of the dependency |
See Also
getExtras
open fun getExtras(): Bundle!
Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one. You can use this to get and set individual extra key/value pairs.
getFragment
open fun getFragment(): String!
Return the fragment class name associated with this preference.
Return | |
---|---|
String!: The fragment class name last set via setFragment or XML |
getIcon
open fun getIcon(): Drawable!
Returns the icon of this preference.
Return | |
---|---|
Drawable!: The icon |
See Also
getIntent
open fun getIntent(): Intent!
Return the Intent
associated with this preference.
Return | |
---|---|
Intent!: The Intent last set via setIntent(Intent) or XML |
getKey
open fun getKey(): String!
Gets the key for this preference, which is also the key used for storing values into SharedPreferences
or PreferenceDataStore
.
Return | |
---|---|
String!: The key |
getLayoutResource
fun getLayoutResource(): Int
Gets the layout resource that will be shown as the View
for this preference.
Return | |
---|---|
Int: The layout resource ID |
getOnPreferenceChangeListener
open fun getOnPreferenceChangeListener(): Preference.OnPreferenceChangeListener!
Returns the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).
Return | |
---|---|
Preference.OnPreferenceChangeListener!: The callback to be invoked |
getOnPreferenceClickListener
open fun getOnPreferenceClickListener(): Preference.OnPreferenceClickListener!
Returns the callback to be invoked when this preference is clicked.
Return | |
---|---|
Preference.OnPreferenceClickListener!: The callback to be invoked |
getOrder
open fun getOrder(): Int
Gets the order of this preference with respect to other preference objects on the same level.
Return | |
---|---|
Int: The order of this preference |
See Also
getParent
@Nullable open fun getParent(): 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.
Return | |
---|---|
PreferenceGroup?: The parent PreferenceGroup or null if not attached to any |
getPersistedStringSet
open fun getPersistedStringSet(defaultReturnValue: MutableSet<String!>!): MutableSet<String!>!
Attempts to get a persisted set of Strings if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
MutableSet<String!>!: The default value to return if either this preference is not persistent or this preference is not present. |
Return | |
---|---|
MutableSet<String!>!: The value from the storage or the default return value |
See Also
getPreferenceDataStore
@Nullable open fun getPreferenceDataStore(): PreferenceDataStore?
Returns PreferenceDataStore
used by this preference. Returns null
if SharedPreferences
is used instead.
By default preferences always use SharedPreferences
. To make this preference to use the PreferenceDataStore
you need to assign your implementation to the preference itself via setPreferenceDataStore(PreferenceDataStore)
or to its PreferenceManager
via PreferenceManager#setPreferenceDataStore(PreferenceDataStore)
.
Return | |
---|---|
PreferenceDataStore?: The PreferenceDataStore used by this preference or null if none |
getPreferenceManager
open fun getPreferenceManager(): PreferenceManager!
Gets the PreferenceManager
that manages this preference object's tree.
Return | |
---|---|
PreferenceManager!: The PreferenceManager |
getSharedPreferences
open fun getSharedPreferences(): SharedPreferences!
Returns the SharedPreferences
where this preference can read its value(s). Usually, it's easier to use one of the helper read methods: getPersistedBoolean(boolean)
, getPersistedFloat(float)
, getPersistedInt(int)
, getPersistedLong(long)
, getPersistedString(String)
.
Return | |
---|---|
SharedPreferences!: The SharedPreferences where this preference reads its value(s). If this preference is not attached to a preference hierarchy or if a PreferenceDataStore has been set, this method returns null . |
getShouldDisableView
open fun getShouldDisableView(): Boolean
Checks whether this preference should disable its view when it's action is disabled.
Return | |
---|---|
Boolean: true if it should disable the view |
See Also
getSummary
open fun getSummary(): CharSequence!
Returns the summary of this preference. If a SummaryProvider
has been set for this preference, it will be used to provide the summary returned by this method.
Return | |
---|---|
CharSequence!: The summary |
getSummaryProvider
@Nullable fun getSummaryProvider(): Preference.SummaryProvider<Preference!>?
Returns the SummaryProvider
used to configure the summary of this preference.
Return | |
---|---|
Preference.SummaryProvider<Preference!>?: The SummaryProvider used to configure the summary of this preference, or null if there is no SummaryProvider set |
See Also
getTitle
open fun getTitle(): CharSequence!
Returns the title of this preference.
Return | |
---|---|
CharSequence!: The title |
See Also
getWidgetLayoutResource
fun getWidgetLayoutResource(): Int
Gets the layout resource for the controllable widget portion of this preference.
Return | |
---|---|
Int: The layout resource ID |
hasKey
open fun hasKey(): Boolean
Checks whether this preference has a valid key.
Return | |
---|---|
Boolean: true if the key exists and is not a blank string, false otherwise |
isCopyingEnabled
open fun isCopyingEnabled(): Boolean
Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference.
Return | |
---|---|
Boolean: true if copying is enabled, false otherwise |
isEnabled
open fun isEnabled(): Boolean
Checks whether this preference should be enabled in the list.
Return | |
---|---|
Boolean: true if this preference is enabled, false otherwise |
isIconSpaceReserved
open fun isIconSpaceReserved(): Boolean
Returns whether the space of this preference icon view is reserved.
Return | |
---|---|
Boolean: true if the space of this preference icon view is reserved android.R.attr#iconSpaceReserved |
See Also
isPersistent
open fun isPersistent(): Boolean
Checks whether this preference is persistent. If it is, it stores its value(s) into the persistent SharedPreferences
storage by default or into PreferenceDataStore
if assigned.
Return | |
---|---|
Boolean: true if persistent |
isSelectable
open fun isSelectable(): Boolean
Checks whether this preference should be selectable in the list.
Return | |
---|---|
Boolean: true if it is selectable, false otherwise |
isShown
fun isShown(): Boolean
Checks whether this preference is shown to the user in the hierarchy. For a preference to be shown in the hierarchy, it and all of its ancestors must be visible and attached to the root PreferenceScreen
.
Return | |
---|---|
Boolean: true if this preference is shown to the user in the hierarchy |
isSingleLineTitle
open fun isSingleLineTitle(): Boolean
Gets whether the title of this preference is constrained to a single line.
Return | |
---|---|
Boolean: true if the title of this preference is constrained to a single line android.R.attr#singleLineTitle |
See Also
isVisible
fun isVisible(): Boolean
Checks whether this preference should be visible to the user. If this preference is visible, but one or more of its ancestors are not visible, then this preference will not be shown until its ancestors are all visible.
Return | |
---|---|
Boolean: true if this preference should be displayed |
See Also
notifyDependencyChange
open 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
open 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.
onBindViewHolder
open fun onBindViewHolder(holder: PreferenceViewHolder!): Unit
Binds the created View to the data for this preference.
This is a good place to grab references to custom Views in the layout and set properties on them.
Make sure to call through to the superclass's implementation.
Parameters | |
---|---|
holder |
PreferenceViewHolder!: The ViewHolder that provides references to the views to fill in. These views will be recycled, so you should not hold a reference to them after this method returns. |
onDependencyChanged
open fun onDependencyChanged(dependency: Preference!, disableDependent: Boolean): Unit
Called when the dependency changes.
Parameters | |
---|---|
dependency |
Preference!: The preference that this preference depends on |
disableDependent |
Preference!: Set true to disable this preference |
onDetached
open 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.
onInitializeAccessibilityNodeInfo
@CallSuper open funonInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfoCompat!): Unit
Deprecated: Preferences aren't views. They should not need any accessibility changes, unless the view hierarchy is customized. In this situation, please add Accessibility information in onBindViewHolder
.
Initializes an android.view.accessibility.AccessibilityNodeInfo
with information about the View for this preference.
onParentChanged
open fun onParentChanged(parent: Preference!, disableChild: Boolean): Unit
Called when the implicit parent dependency changes.
Parameters | |
---|---|
parent |
Preference!: The preference that this preference depends on |
disableChild |
Preference!: Set true to disable this preference |
peekExtras
open fun peekExtras(): Bundle!
Return the extras Bundle object associated with this preference, returning null
if there is not currently one.
persistStringSet
open fun persistStringSet(values: MutableSet<String!>!): Boolean
Attempts to persist a set of Strings if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
values |
MutableSet<String!>!: The values to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
See Also
restoreHierarchyState
open fun restoreHierarchyState(container: Bundle!): Unit
Restore this preference hierarchy's previously saved state from the given container.
Parameters | |
---|---|
container |
Bundle!: The Bundle that holds the previously saved state |
saveHierarchyState
open fun saveHierarchyState(container: Bundle!): Unit
Store this preference hierarchy's frozen state into the given container.
Parameters | |
---|---|
container |
Bundle!: The Bundle in which to save the instance of this preference |
setCopyingEnabled
open fun setCopyingEnabled(enabled: Boolean): Unit
Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference.
Parameters | |
---|---|
enabled |
Boolean: Set true to enable copying the summary of this preference |
setDefaultValue
open fun setDefaultValue(defaultValue: Any!): Unit
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.
Parameters | |
---|---|
defaultValue |
Any!: The default value |
setDependency
open fun setDependency(dependencyKey: String!): Unit
Sets the key of a preference that this preference will depend on. If that preference is not set or is off, this preference will be disabled.
Parameters | |
---|---|
dependencyKey |
String!: The key of the preference that this depends on |
setEnabled
open fun setEnabled(enabled: Boolean): Unit
Sets whether this preference is enabled. If disabled, it will not handle clicks.
Parameters | |
---|---|
enabled |
Boolean: Set true to enable it |
setFragment
open fun setFragment(fragment: String!): Unit
Sets the class name of a fragment to be shown when this preference is clicked.
Parameters | |
---|---|
fragment |
String!: The class name of the fragment associated with this preference |
setIcon
open fun setIcon(icon: Drawable!): Unit
Sets the icon for this preference with a Drawable. This icon will be placed into the ID android.R.id#icon
within the View created by onBindViewHolder(PreferenceViewHolder)
.
Parameters | |
---|---|
icon |
Drawable!: The optional icon for this preference |
setIcon
open fun setIcon(iconResId: Int): Unit
Sets the icon for this preference with a resource ID.
Parameters | |
---|---|
iconResId |
Int: The icon as a resource ID |
See Also
setIconSpaceReserved
open fun setIconSpaceReserved(iconSpaceReserved: Boolean): Unit
Sets whether to reserve the space of this preference icon view when no icon is provided. If set to true, the preference will be offset as if it would have the icon and thus aligned with other preferences having icons.
Parameters | |
---|---|
iconSpaceReserved |
Boolean: Set true if the space for the icon view should be reserved android.R.attr#iconSpaceReserved |
setIntent
open fun setIntent(intent: Intent!): Unit
Sets an Intent
to be used for Context#startActivity(Intent)
when this preference is clicked.
Parameters | |
---|---|
intent |
Intent!: The intent associated with this preference |
setKey
open fun setKey(key: String!): Unit
Sets the key for this preference, which is used as a key to the SharedPreferences
or PreferenceDataStore
. This should be unique for the package.
Parameters | |
---|---|
key |
String!: The key for the preference |
setLayoutResource
open fun setLayoutResource(layoutResId: Int): Unit
Sets the layout resource that is inflated as the View
to be shown for this preference. In most cases, the default layout is sufficient for custom preference objects and only the widget layout needs to be changed.
This layout should contain a ViewGroup
with ID android.R.id#widget_frame
to be the parent of the specific widget for this preference. It should similarly contain android.R.id#title
and android.R.id#summary
.
It is an error to change the layout after adding the preference to a PreferenceGroup
.
Parameters | |
---|---|
layoutResId |
Int: The layout resource ID to be inflated and returned as a View |
See Also
setOnPreferenceChangeListener
open fun setOnPreferenceChangeListener(onPreferenceChangeListener: Preference.OnPreferenceChangeListener!): Unit
Sets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).
Parameters | |
---|---|
onPreferenceChangeListener |
Preference.OnPreferenceChangeListener!: The callback to be invoked |
setOnPreferenceClickListener
open fun setOnPreferenceClickListener(onPreferenceClickListener: Preference.OnPreferenceClickListener!): Unit
Sets the callback to be invoked when this preference is clicked.
Parameters | |
---|---|
onPreferenceClickListener |
Preference.OnPreferenceClickListener!: The callback to be invoked |
setOrder
open fun setOrder(order: Int): Unit
Sets the order of this preference with respect to other preference objects on the same level. If this is not specified, the default behavior is to sort alphabetically. The PreferenceGroup#setOrderingAsAdded(boolean)
can be used to order preference objects based on the order they appear in the XML.
Parameters | |
---|---|
order |
Int: The order for this preference. A lower value will be shown first. Use DEFAULT_ORDER to sort alphabetically or allow ordering from XML. |
setPersistent
open fun setPersistent(persistent: Boolean): Unit
Sets whether this preference is persistent. When persistent, it stores its value(s) into the persistent SharedPreferences
storage by default or into PreferenceDataStore
if assigned.
Parameters | |
---|---|
persistent |
Boolean: Set true if it should store its value(s) into the storage |
setPreferenceDataStore
open fun setPreferenceDataStore(dataStore: PreferenceDataStore!): Unit
Sets a PreferenceDataStore
to be used by this preference instead of using SharedPreferences
.
The data store will remain assigned even if the preference is moved around the preference hierarchy. It will also override a data store propagated from the PreferenceManager
that owns this preference.
Parameters | |
---|---|
dataStore |
PreferenceDataStore!: The PreferenceDataStore to be used by this preference |
setSelectable
open fun setSelectable(selectable: Boolean): Unit
Sets whether this preference is selectable.
Parameters | |
---|---|
selectable |
Boolean: Set true to make it selectable |
setShouldDisableView
open fun setShouldDisableView(shouldDisableView: Boolean): Unit
Sets whether this preference should disable its view when it gets disabled.
For example, set this and setEnabled(boolean)
to false for preferences that are only displaying information and 1) should not be clickable 2) should not have the view set to the disabled state.
Parameters | |
---|---|
shouldDisableView |
Boolean: Set true if this preference should disable its view when the preference is disabled. |
setSingleLineTitle
open fun setSingleLineTitle(singleLineTitle: Boolean): Unit
Sets whether to constrain the title of this preference to a single line instead of letting it wrap onto multiple lines.
Parameters | |
---|---|
singleLineTitle |
Boolean: Set true if the title should be constrained to one line android.R.attr#singleLineTitle |
setSummary
open fun setSummary(summary: CharSequence!): Unit
Sets the summary for this preference with a CharSequence.
You can also use a SummaryProvider
to dynamically configure the summary of this preference.
Parameters | |
---|---|
summary |
CharSequence!: The summary for the preference |
Exceptions | |
---|---|
IllegalStateException |
If a SummaryProvider has already been set. |
See Also
setSummary
open fun setSummary(summaryResId: Int): Unit
Sets the summary for this preference with a resource ID.
You can also use a SummaryProvider
to dynamically configure the summary of this preference.
Parameters | |
---|---|
summaryResId |
Int: The summary as a resource |
setSummaryProvider
fun setSummaryProvider(@Nullable summaryProvider: Preference.SummaryProvider<Preference!>?): Unit
Set a SummaryProvider
that will be invoked whenever the summary of this preference is requested. Set null
to remove the existing SummaryProvider.
Parameters | |
---|---|
summaryProvider |
Preference.SummaryProvider<Preference!>?: The SummaryProvider that will be invoked whenever the summary of this preference is requested |
See Also
setTitle
open fun setTitle(title: CharSequence!): Unit
Sets the title for this preference with a CharSequence. This title will be placed into the ID android.R.id#title
within the View bound by onBindViewHolder(PreferenceViewHolder)
.
Parameters | |
---|---|
title |
CharSequence!: The title for this preference |
setTitle
open fun setTitle(titleResId: Int): Unit
Sets the title for this preference with a resource ID.
Parameters | |
---|---|
titleResId |
Int: The title as a resource ID |
See Also
setViewId
open fun setViewId(viewId: Int): Unit
Set the ID that will be assigned to the overall View representing this preference, once bound.
See Also
setVisible
fun setVisible(visible: Boolean): Unit
Sets whether this preference should be visible to the user. If false, it is excluded from the adapter, but can still be retrieved using PreferenceFragmentCompat#findPreference(CharSequence)
.
To show this preference to the user, its ancestors must also all be visible. If you make a PreferenceGroup
invisible, none of its children will be shown to the user until the group is visible.
Parameters | |
---|---|
visible |
Boolean: Set false if this preference should be hidden from the user androidx.preference.R.attr#isPreferenceVisible |
See Also
setWidgetLayoutResource
open fun setWidgetLayoutResource(widgetLayoutResId: Int): Unit
Sets the layout for the controllable widget portion of this preference. This is inflated into the main layout. For example, a CheckBoxPreference
would specify a custom layout (consisting of just the CheckBox) here, instead of creating its own main layout.
It is an error to change the layout after adding the preference to a PreferenceGroup
.
Parameters | |
---|---|
widgetLayoutResId |
Int: The layout resource ID to be inflated into the main layout |
See Also
shouldDisableDependents
open fun shouldDisableDependents(): Boolean
Checks whether this preference's dependents should currently be disabled.
Return | |
---|---|
Boolean: true if the dependents should be disabled, otherwise false |
toString
open fun toString(): String
Protected methods
findPreferenceInHierarchy
@Nullable protected open fun <T : Preference!> findPreferenceInHierarchy(@NonNull key: String): T?
Finds a preference in the entire hierarchy (above or below this preference) with the given key. Returns null
if no preference could be found with the given key.
This only works after this preference has been attached to a hierarchy.
Parameters | |
---|---|
key |
String: The key of the preference to retrieve |
Return | |
---|---|
T?: The preference with the key, or null |
getPersistedBoolean
protected open fun getPersistedBoolean(defaultReturnValue: Boolean): Boolean
Attempts to get a persisted Boolean
if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
Boolean: The default value to return if either this preference is not persistent or this preference is not in the SharedPreferences. |
Return | |
---|---|
Boolean: The value from the storage or the default return value |
getPersistedFloat
protected open fun getPersistedFloat(defaultReturnValue: Float): Float
Attempts to get a persisted Float
if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
Float: The default value to return if either this preference is not persistent or this preference is not saved. |
Return | |
---|---|
Float: The value from the storage or the default return value |
getPersistedInt
protected open fun getPersistedInt(defaultReturnValue: Int): Int
Attempts to get a persisted Integer
if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
Int: The default value to return if either this preference is not persistent or this preference is not in the SharedPreferences. |
Return | |
---|---|
Int: The value from the storage or the default return value |
getPersistedLong
protected open fun getPersistedLong(defaultReturnValue: Long): Long
Attempts to get a persisted Long
if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
Long: The default value to return if either this preference is not persistent or this preference is not in the SharedPreferences. |
Return | |
---|---|
Long: The value from the storage or the default return value |
getPersistedString
protected open fun getPersistedString(defaultReturnValue: String!): String!
Attempts to get a persisted set of Strings if this preference is persistent.
Parameters | |
---|---|
defaultReturnValue |
String!: The default value to return if either the preference is not persistent or the preference is not in the shared preferences. |
Return | |
---|---|
String!: The value from the storage or the default return value |
See Also
notifyChanged
protected open fun notifyChanged(): Unit
Should be called when the data of this Preference
has changed.
notifyHierarchyChanged
protected open fun notifyHierarchyChanged(): Unit
Should be called when a preference has been added/removed from this group, or the ordering should be re-evaluated.
onAttachedToHierarchy
protected open fun onAttachedToHierarchy(preferenceManager: PreferenceManager!): Unit
Called when this preference has been attached to a preference hierarchy. Make sure to call the super implementation.
Parameters | |
---|---|
preferenceManager |
PreferenceManager!: The PreferenceManager of the hierarchy |
onClick
protected open fun onClick(): Unit
Processes a click on the preference. This includes saving the value to the SharedPreferences
. However, the overridden method should call callChangeListener(Object)
to make sure the client wants to update the preference's state with the new value.
onGetDefaultValue
protected open fun onGetDefaultValue(a: TypedArray!, index: Int): Any!
Called when a preference is being inflated and the default value attribute needs to be read. Since different preference types have different value types, the subclass should get and return the default value which will be its value type.
For example, if the value type is String, the body of the method would proxy to TypedArray#getString(int)
.
Parameters | |
---|---|
a |
TypedArray!: The set of attributes |
index |
TypedArray!: The index of the default value attribute |
Return | |
---|---|
Any!: The default value of this preference type |
onPrepareForRemoval
protected open fun onPrepareForRemoval(): Unit
Called when this preference is being removed from the hierarchy. You should remove any references to this preference that you know about. Make sure to call through to the superclass implementation.
onRestoreInstanceState
protected open 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 open 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.
Return | |
---|---|
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 . |
onSetInitialValue
protected open funonSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any!): Unit
Deprecated: Use onSetInitialValue(Object)
instead.
Implement this to set the initial value of the preference.
If restorePersistedValue is true, you should restore the preference value from the SharedPreferences
. If restorePersistedValue is false, you should set the preference value to defaultValue that is given (and possibly store to SharedPreferences if shouldPersist()
is true).
In case of using PreferenceDataStore
, the restorePersistedValue is always true
but the default value (if provided) is set.
This may not always be called. One example is if it should not persist but there is no default value given.
Parameters | |
---|---|
restorePersistedValue |
Boolean: True to restore the persisted value; false to use the given defaultValue. |
defaultValue |
Boolean: The default value for this preference. Only use this if restorePersistedValue is false. |
onSetInitialValue
protected open fun onSetInitialValue(@Nullable defaultValue: Any?): Unit
Implement this to set the initial value of the preference.
If you are persisting values to SharedPreferences
or a PreferenceDataStore
you should restore the saved value for the preference.
If you are not persisting values, or there is no value saved for the preference, you should set the value of the preference to defaultValue.
Parameters | |
---|---|
defaultValue |
Any?: The default value for the preference if set, otherwise null . |
persistBoolean
protected open fun persistBoolean(value: Boolean): Boolean
Attempts to persist a Boolean
if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
value |
Boolean: The value to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
persistFloat
protected open fun persistFloat(value: Float): Boolean
Attempts to persist a Float
if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
value |
Float: The value to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
persistInt
protected open fun persistInt(value: Int): Boolean
Attempts to persist an Integer
if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
value |
Int: The value to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
persistLong
protected open fun persistLong(value: Long): Boolean
Attempts to persist a Long
if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
value |
Long: The value to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
persistString
protected open fun persistString(value: String!): Boolean
Attempts to persist a String
if this preference is persistent.
The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.
Parameters | |
---|---|
value |
String!: The value to persist |
Return | |
---|---|
Boolean: true if the preference is persistent, false otherwise |
See Also
shouldPersist
protected open fun shouldPersist(): 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. This, at minimum, checks whether this preference is persistent and it currently has a key. Before you save/restore from the storage, check this first.
Return | |
---|---|
Boolean: true if it should persist the value |