Android APIs
public class

SwitchPreference

extends TwoStatePreference
java.lang.Object
   ↳ android.preference.Preference
     ↳ android.preference.TwoStatePreference
       ↳ android.preference.SwitchPreference

Class Overview

A Preference that provides a two-state toggleable option.

This preference will store a boolean into the SharedPreferences.

Summary

XML Attributes
Attribute Name Related Method Description
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. 
[Expand]
Inherited XML Attributes
From class android.preference.Preference
[Expand]
Inherited Constants
From class android.preference.Preference
Public Constructors
SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Construct a new SwitchPreference with the given style options.
SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)
Construct a new SwitchPreference with the given style options.
SwitchPreference(Context context, AttributeSet attrs)
Construct a new SwitchPreference with the given style options.
SwitchPreference(Context context)
Construct a new SwitchPreference with default style options.
Public Methods
CharSequence getSwitchTextOff()
CharSequence getSwitchTextOn()
void setSwitchTextOff(int resId)
Set the text displayed on the switch widget in the off state.
void setSwitchTextOff(CharSequence offText)
Set the text displayed on the switch widget in the off state.
void setSwitchTextOn(int resId)
Set the text displayed on the switch widget in the on state.
void setSwitchTextOn(CharSequence onText)
Set the text displayed on the switch widget in the on state.
Protected Methods
void onBindView(View view)
Binds the created View to the data for this Preference.
[Expand]
Inherited Methods
From class android.preference.TwoStatePreference
From class android.preference.Preference
From class java.lang.Object
From interface java.lang.Comparable

XML Attributes

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.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol disableDependentsState.

Related Methods

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.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol summaryOff.

Related Methods

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.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol summaryOn.

Related Methods

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.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol switchTextOff.

Related Methods

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.

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol switchTextOn.

Related Methods

Public Constructors

public SwitchPreference (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

Construct a new SwitchPreference with the given style options.

Parameters
context The Context that will style this preference
attrs Style attributes that differ from the default
defStyleAttr 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 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 SwitchPreference (Context context, AttributeSet attrs, int defStyleAttr)

Added in API level 14

Construct a new SwitchPreference with the given style options.

Parameters
context The Context that will style this preference
attrs Style attributes that differ from the default
defStyleAttr 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.

public SwitchPreference (Context context, AttributeSet attrs)

Added in API level 14

Construct a new SwitchPreference with the given style options.

Parameters
context The Context that will style this preference
attrs Style attributes that differ from the default

public SwitchPreference (Context context)

Added in API level 14

Construct a new SwitchPreference with default style options.

Parameters
context The Context that will style this preference

Public Methods

public CharSequence getSwitchTextOff ()

Added in API level 14

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

public CharSequence getSwitchTextOn ()

Added in API level 14

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

public void setSwitchTextOff (int resId)

Added in API level 14

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 The text as a string resource ID

public void setSwitchTextOff (CharSequence offText)

Added in API level 14

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 Text to display in the off state

public void setSwitchTextOn (int resId)

Added in API level 14

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 The text as a string resource ID

public void setSwitchTextOn (CharSequence onText)

Added in API level 14

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 Text to display in the on state

Protected Methods

protected void onBindView (View view)

Added in API level 14

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
view The View that shows this Preference.