SwitchPreference

class SwitchPreference : TwoStatePreference


A Preference that provides a two-state toggleable option.

This preference will save a boolean value to android.content.SharedPreferences.

name android:summaryOff

name android:summaryOn

name android:switchTextOff

name android:switchTextOn

name android:disableDependentsState

Summary

Public constructors

Construct a new SwitchPreference with default style options.

SwitchPreference(context: Context, attrs: AttributeSet?)

Construct a new SwitchPreference with the given style options.

SwitchPreference(context: Context, attrs: AttributeSet?, defStyleAttr: Int)

Construct a new SwitchPreference with the given style options.

SwitchPreference(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
)

Construct a new SwitchPreference with the given style options.

Public functions

CharSequence?
CharSequence?
Unit

Binds the created View to the data for this preference.

Unit

Set the text displayed on the switch widget in the off state.

Unit

Set the text displayed on the switch widget in the off state.

Unit

Set the text displayed on the switch widget in the on state.

Unit

Set the text displayed on the switch widget in the on state.

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

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

Unit

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

Unit

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

Unit

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

Unit
onDependencyChanged(dependency: Preference, disableDependent: Boolean)

Called when the dependency changes.

Unit

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

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, 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
From androidx.preference.TwoStatePreference
Boolean

Returns whether dependents are disabled when this preference is on (true) or when this preference is off (false).

CharSequence?

Returns the summary to be shown when unchecked.

CharSequence?

Returns the summary to be shown when checked.

Boolean

Returns the checked state.

Unit

Processes a click on the preference.

Any?

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

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.

Unit
onSetInitialValue(defaultValue: Any!)

Implement this to set the initial value of the preference.

Unit
setChecked(checked: Boolean)

Sets the checked state and saves it.

Unit
setDisableDependentsState(disableDependentsState: Boolean)

Sets whether dependents are disabled when this preference is on (true) or when this preference is off (false).

Unit

Sets the summary to be shown when unchecked.

Unit
setSummaryOff(summaryResId: Int)
Unit

Sets the summary to be shown when checked.

Unit
setSummaryOn(summaryResId: Int)
Boolean

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

Unit

Sync a summary holder contained within holder's sub-hierarchy with the correct summary text.

Public constructors

SwitchPreference

Added in 1.0.0
SwitchPreference(context: Context)

Construct a new SwitchPreference with default style options.

Parameters
context: Context

The Context that will style this preference

SwitchPreference

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

Construct a new SwitchPreference with the given style options.

Parameters
context: Context

The Context that will style this preference

attrs: AttributeSet?

Style attributes that differ from the default

SwitchPreference

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

Construct a new SwitchPreference with the given style options.

Parameters
context: Context

The Context that will style this preference

attrs: AttributeSet?

Style attributes that differ from the default

defStyleAttr: Int

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.

SwitchPreference

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

Construct a new SwitchPreference with the given style options.

Parameters
context: Context

The Context that will style this preference

attrs: AttributeSet?

Style attributes that differ from the default

defStyleAttr: Int

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: Int

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.

Public functions

getSwitchTextOff

Added in 1.0.0
fun getSwitchTextOff(): CharSequence?
Returns
CharSequence?

The text that will be displayed on the switch widget in the off state

getSwitchTextOn

Added in 1.0.0
fun getSwitchTextOn(): CharSequence?
Returns
CharSequence?

The text that will be displayed on the switch widget in the on state

onBindViewHolder

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.

setSwitchTextOff

Added in 1.0.0
fun setSwitchTextOff(offText: CharSequence?): Unit

Set the text displayed on the switch widget in the off state. This should be a very short string, one word if possible.

Parameters
offText: CharSequence?

Text to display in the off state

setSwitchTextOff

Added in 1.0.0
fun setSwitchTextOff(resId: Int): Unit

Set the text displayed on the switch widget in the off state. This should be a very short string, one word if possible.

Parameters
resId: Int

The text as a string resource ID

setSwitchTextOn

Added in 1.0.0
fun setSwitchTextOn(onText: CharSequence?): Unit

Set the text displayed on the switch widget in the on state. This should be a very short string, one word if possible.

Parameters
onText: CharSequence?

Text to display in the on state

setSwitchTextOn

Added in 1.0.0
fun setSwitchTextOn(resId: Int): Unit

Set the text displayed on the switch widget in the on state. This should be a very short string, one word if possible.

Parameters
resId: Int

The text as a string resource ID