This preference will store a boolean into the SharedPreferences.
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.
|
Protected methods |
void
|
onBindView(View view)
Binds the created View to the data for this Preference.
|
Inherited methods |
|
From class
android.preference.Preference
boolean
|
callChangeListener(Object newValue)
Call this method after the user changes the preference, but before the
internal state is set.
|
int
|
compareTo(Preference another)
Compares Preference objects based on order (if set), otherwise alphabetically on the titles.
|
Preference
|
findPreferenceInHierarchy(String key)
Finds a Preference in this hierarchy (the whole thing,
even above/below your PreferenceScreen screen break) with the given
key.
|
Context
|
getContext()
Returns the Context of this Preference.
|
String
|
getDependency()
Returns the key of the dependency on this Preference.
|
SharedPreferences.Editor
|
getEditor()
Returns an SharedPreferences.Editor where this Preference can
save its value(s).
|
Bundle
|
getExtras()
Return the extras Bundle object associated with this preference, creating
a new Bundle if there currently isn't one.
|
String
|
getFragment()
Return the fragment class name associated with this Preference.
|
Drawable
|
getIcon()
Returns the icon of this Preference.
|
Intent
|
getIntent()
Return the Intent associated with this Preference.
|
String
|
getKey()
Gets the key for this Preference, which is also the key used for storing values into
SharedPreferences or PreferenceDataStore .
|
int
|
getLayoutResource()
Gets the layout resource that will be shown as the View for this Preference.
|
Preference.OnPreferenceChangeListener
|
getOnPreferenceChangeListener()
Returns the callback to be invoked when this Preference is changed by the
user (but before the internal state has been updated).
|
Preference.OnPreferenceClickListener
|
getOnPreferenceClickListener()
Returns the callback to be invoked when this Preference is clicked.
|
int
|
getOrder()
Gets the order of this Preference with respect to other Preference objects on the same level.
|
PreferenceGroup
|
getParent()
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(boolean defaultReturnValue)
Attempts to get a persisted boolean if this Preference is persistent.
|
float
|
getPersistedFloat(float defaultReturnValue)
Attempts to get a persisted float if this Preference is persistent.
|
int
|
getPersistedInt(int defaultReturnValue)
Attempts to get a persisted int if this Preference is persistent.
|
long
|
getPersistedLong(long defaultReturnValue)
Attempts to get a persisted long if this Preference is persistent.
|
String
|
getPersistedString(String defaultReturnValue)
Attempts to get a persisted String if this Preference is persistent.
|
Set<String>
|
getPersistedStringSet(Set<String> defaultReturnValue)
Attempts to get a persisted set of Strings if this Preference is persistent.
|
PreferenceDataStore
|
getPreferenceDataStore()
Returns PreferenceDataStore used by this Preference.
|
PreferenceManager
|
getPreferenceManager()
Gets the PreferenceManager that manages this Preference object's tree.
|
SharedPreferences
|
getSharedPreferences()
Returns the SharedPreferences where this Preference can read its
value(s).
|
boolean
|
getShouldDisableView()
Checks whether this Preference should disable its view when it's action is disabled.
|
CharSequence
|
getSummary()
Returns the summary of this Preference.
|
CharSequence
|
getTitle()
Returns the title of this Preference.
|
int
|
getTitleRes()
Returns the title resource ID of this Preference.
|
View
|
getView(View convertView, ViewGroup parent)
Gets the View that will be shown in the PreferenceActivity .
|
int
|
getWidgetLayoutResource()
Gets the layout resource for the controllable widget portion of this Preference.
|
boolean
|
hasKey()
Checks whether this Preference has a valid key.
|
boolean
|
isEnabled()
Checks whether this Preference should be enabled in the list.
|
boolean
|
isIconSpaceReserved()
Gets whether the space this preference icon view is reserved.
|
boolean
|
isPersistent()
Checks whether this Preference is persistent.
|
boolean
|
isRecycleEnabled()
Checks whether this Preference has enabled to have its view recycled when used in the list
view.
|
boolean
|
isSelectable()
Checks whether this Preference should be selectable in the list.
|
boolean
|
isSingleLineTitle()
Gets whether the title of this preference is constrained to a single line.
|
void
|
notifyChanged()
Should be called when the data of this Preference has changed.
|
void
|
notifyDependencyChange(boolean disableDependents)
Notifies any listening dependents of a change that affects the
dependency.
|
void
|
notifyHierarchyChanged()
Should be called when a Preference has been
added/removed from this group, or the ordering should be
re-evaluated.
|
void
|
onAttachedToActivity()
Called when the Preference hierarchy has been attached to the
PreferenceActivity .
|
void
|
onAttachedToHierarchy(PreferenceManager preferenceManager)
Called when this Preference has been attached to a Preference hierarchy.
|
void
|
onBindView(View view)
Binds the created View to the data for this Preference.
|
void
|
onClick()
Processes a click on the preference.
|
View
|
onCreateView(ViewGroup parent)
Creates the View to be shown for this Preference in the
PreferenceActivity .
|
void
|
onDependencyChanged(Preference dependency, boolean disableDependent)
Called when the dependency changes.
|
Object
|
onGetDefaultValue(TypedArray a, int index)
Called when a Preference is being inflated and the default value
attribute needs to be read.
|
void
|
onParentChanged(Preference parent, boolean disableChild)
Called when the implicit parent dependency changes.
|
void
|
onPrepareForRemoval()
Called when this Preference is being removed from the hierarchy.
|
void
|
onRestoreInstanceState(Parcelable state)
Hook allowing a Preference to re-apply a representation of its internal state that had
previously been generated by onSaveInstanceState() .
|
Parcelable
|
onSaveInstanceState()
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.
|
void
|
onSetInitialValue(boolean restorePersistedValue, Object defaultValue)
Implement this to set the initial value of the Preference.
|
Bundle
|
peekExtras()
Return the extras Bundle object associated with this preference, returning null if
there is not currently one.
|
boolean
|
persistBoolean(boolean value)
Attempts to persist a boolean if this Preference is persistent.
|
boolean
|
persistFloat(float value)
Attempts to persist a long if this Preference is persistent.
|
boolean
|
persistInt(int value)
Attempts to persist an int if this Preference is persistent.
|
boolean
|
persistLong(long value)
Attempts to persist a long if this Preference is persistent.
|
boolean
|
persistString(String value)
Attempts to persist a String if this Preference is persistent.
|
boolean
|
persistStringSet(Set<String> values)
Attempts to persist a set of Strings if this Preference is persistent.
|
void
|
restoreHierarchyState(Bundle container)
Restore this Preference hierarchy's previously saved state from the given container.
|
void
|
saveHierarchyState(Bundle container)
Store this Preference hierarchy's frozen state into the given container.
|
void
|
setDefaultValue(Object defaultValue)
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.
|
void
|
setDependency(String dependencyKey)
Sets the key of a Preference that this Preference will depend on.
|
void
|
setEnabled(boolean enabled)
Sets whether this Preference is enabled.
|
void
|
setFragment(String fragment)
Sets the class name of a fragment to be shown when this Preference is clicked.
|
void
|
setIcon(Drawable icon)
Sets the icon for this Preference with a Drawable.
|
void
|
setIcon(int iconResId)
Sets the icon for this Preference with a resource ID.
|
void
|
setIconSpaceReserved(boolean iconSpaceReserved)
Sets whether to reserve the space of this Preference icon view when no icon is provided.
|
void
|
setIntent(Intent intent)
Sets an Intent to be used for
Context#startActivity(Intent) when this Preference is clicked.
|
void
|
setKey(String key)
Sets the key for this Preference, which is used as a key to the SharedPreferences or
PreferenceDataStore .
|
void
|
setLayoutResource(int layoutResId)
Sets the layout resource that is inflated as the View to be shown
for this Preference.
|
void
|
setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener)
Sets the callback to be invoked when this Preference is changed by the
user (but before the internal state has been updated).
|
void
|
setOnPreferenceClickListener(Preference.OnPreferenceClickListener onPreferenceClickListener)
Sets the callback to be invoked when this Preference is clicked.
|
void
|
setOrder(int order)
Sets the order of this Preference with respect to other Preference objects on the same level.
|
void
|
setPersistent(boolean persistent)
Sets whether this Preference is persistent.
|
void
|
setPreferenceDataStore(PreferenceDataStore dataStore)
Sets a PreferenceDataStore to be used by this Preference instead of using
SharedPreferences .
|
void
|
setRecycleEnabled(boolean enabled)
Sets whether this Preference has enabled to have its view recycled when used in the list
view.
|
void
|
setSelectable(boolean selectable)
Sets whether this Preference is selectable.
|
void
|
setShouldDisableView(boolean shouldDisableView)
Sets whether this Preference should disable its view when it gets disabled.
|
void
|
setSingleLineTitle(boolean singleLineTitle)
Sets whether to constrain the title of this Preference to a single line instead of
letting it wrap onto multiple lines.
|
void
|
setSummary(CharSequence summary)
Sets the summary for this Preference with a CharSequence.
|
void
|
setSummary(int summaryResId)
Sets the summary for this Preference with a resource ID.
|
void
|
setTitle(CharSequence title)
Sets the title for this Preference with a CharSequence.
|
void
|
setTitle(int titleResId)
Sets the title for this Preference with a resource ID.
|
void
|
setWidgetLayoutResource(int widgetLayoutResId)
Sets the layout for the controllable widget portion of this Preference.
|
boolean
|
shouldCommit()
Returns whether the Preference should commit its saved value(s) in
getEditor() .
|
boolean
|
shouldDisableDependents()
Checks whether this preference's dependents should currently be
disabled.
|
boolean
|
shouldPersist()
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
|
toString()
Returns a string representation of the object.
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long timeoutMillis, int nanos)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait(long timeoutMillis)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted.
|
|
|
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.
May be a boolean value, such as "true
" or
"false
".
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
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
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
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
public SwitchPreference (Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)
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. |
SwitchPreference
public SwitchPreference (Context context,
AttributeSet attrs,
int defStyleAttr)
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
public SwitchPreference (Context context,
AttributeSet attrs)
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
public SwitchPreference (Context context)
Construct a new SwitchPreference with default style options.
Parameters |
context |
Context : The Context that will style this preference |
Public methods
getSwitchTextOff
public CharSequence getSwitchTextOff ()
Returns |
CharSequence |
The text that will be displayed on the switch widget in the off state |
getSwitchTextOn
public CharSequence getSwitchTextOn ()
Returns |
CharSequence |
The text that will be displayed on the switch widget in the on state |
setSwitchTextOff
public void setSwitchTextOff (int resId)
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
public void setSwitchTextOff (CharSequence offText)
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
public void setSwitchTextOn (CharSequence onText)
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
public void setSwitchTextOn (int resId)
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 |
Protected methods
onBindView
protected void onBindView (View view)
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.
If you override this method you must call through to the
superclass implementation.
Parameters |
view |
View : The View that shows this Preference. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2024-04-11 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-11 UTC."]]