Added in API level 14
Deprecated in API level 29

SwitchPreference


open class SwitchPreference : TwoStatePreference
kotlin.Any
   ↳ android.preference.Preference
   ↳ android.preference.TwoStatePreference
   ↳ android.preference.SwitchPreference

A Preference that provides a two-state toggleable option.

This preference will store a boolean into the SharedPreferences.

Summary

XML attributes
android:disableDependentsState The state (true for on, or false for off) that causes dependents to be disabled.
android:summaryOff The summary for the Preference in a PreferenceActivity screen when the SwitchPreference is unchecked.
android:summaryOn The summary for the Preference in a PreferenceActivity screen when the SwitchPreference is checked.
android:switchTextOff The text used on the switch itself when in the "off" state.
android:switchTextOn The text used on the switch itself when in the "on" state.
Inherited XML attributes
Inherited constants
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 methods
open CharSequence!

open CharSequence!

open Unit

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

open Unit

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

open Unit

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

open Unit

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

Protected methods
open Unit
onBindView(view: View!)

Inherited functions

XML attributes

android:disableDependentsState

android:disableDependentsState
The state (true for on, or false for off) that causes dependents to be disabled. By default, dependents will be disabled when this is unchecked, so the value of this preference is false.

May be a boolean value, such as "true" or "false".

android:summaryOff

android:summaryOff
The summary for the Preference in a PreferenceActivity screen when the SwitchPreference is unchecked. If separate on/off summaries are not needed, the summary attribute can be used instead.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

android:summaryOn

android:summaryOn
The summary for the Preference in a PreferenceActivity screen when the SwitchPreference is checked. If separate on/off summaries are not needed, the summary attribute can be used instead.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

android:switchTextOff

android:switchTextOff
The text used on the switch itself when in the "off" state. This should be a very SHORT string, as it appears in a small space.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

android:switchTextOn

android:switchTextOn
The text used on the switch itself when in the "on" state. This should be a very SHORT string, as it appears in a small space.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Public constructors

SwitchPreference

Added in API level 14
SwitchPreference(context: Context!)

Construct a new SwitchPreference with default style options.

Parameters
context Context!: The Context that will style this preference

SwitchPreference

Added in API level 14
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 API level 14
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 API level 21
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 methods

getSwitchTextOff

Added in API level 14
open fun getSwitchTextOff(): CharSequence!

Deprecated: Deprecated in Java.

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

getSwitchTextOn

Added in API level 14
open fun getSwitchTextOn(): CharSequence!

Deprecated: Deprecated in Java.

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

setSwitchTextOff

Added in API level 14
open fun setSwitchTextOff(resId: Int): Unit

Deprecated: Deprecated in Java.

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

setSwitchTextOff

Added in API level 14
open fun setSwitchTextOff(offText: CharSequence!): Unit

Deprecated: Deprecated in Java.

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

setSwitchTextOn

Added in API level 14
open fun setSwitchTextOn(resId: Int): Unit

Deprecated: Deprecated in Java.

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

setSwitchTextOn

Added in API level 14
open fun setSwitchTextOn(onText: CharSequence!): Unit

Deprecated: Deprecated in Java.

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

Protected methods

onBindView

Added in API level 14
protected open fun onBindView(view: View!): Unit

Deprecated: Deprecated in Java.

Parameters
view View!: The View that shows this Preference.