java.lang.Object | |
↳ | android.preference.Preference |
Known Direct Subclasses |
Known Indirect Subclasses |
Represents the basic Preference UI building block displayed by a PreferenceActivity in the form of a ListView. This class provides the View to be displayed in the activity and associates with a SharedPreferences to store/retrieve the preference data.
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 will be used as the key into the
SharedPreferences. It is up to the subclass to decide how to store
the value.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Preference.BaseSavedState | A base class for managing the instance state of a Preference. | ||||||||||
Preference.OnPreferenceChangeListener | Interface definition for a callback to be invoked when the value of this Preference has been changed by the user and is about to be set and/or persisted. | ||||||||||
Preference.OnPreferenceClickListener | Interface definition for a callback to be invoked when a Preference is clicked. |
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:defaultValue | The default value for the preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage. | ||||||||||
android:dependency | The key of another Preference that this Preference will depend on. | ||||||||||
android:enabled | Whether the Preference is enabled. | ||||||||||
android:key | The key to store the Preference value. | ||||||||||
android:layout | The layout for the Preference in a PreferenceActivity screen. | ||||||||||
android:order | The order for the Preference (lower values are to be ordered first). | ||||||||||
android:persistent | Whether the Preference stores its value to the shared preferences. | ||||||||||
android:selectable | Whether the Preference is selectable. | ||||||||||
android:shouldDisableView | Whether the view of this Preference should be disabled when this Preference is disabled. | ||||||||||
android:summary | The summary for the Preference in a PreferenceActivity screen. | ||||||||||
android:title | The title for the Preference in a PreferenceActivity screen. | ||||||||||
android:widgetLayout | The layout for the controllable widget portion of a Preference. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | DEFAULT_ORDER | Specify for setOrder(int) if a specific order is not required. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Perform inflation from XML and apply a class-specific base style.
| |||||||||||
Constructor that is called when inflating a Preference from XML.
| |||||||||||
Constructor to create a Preference.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Compares Preference objects based on order (if set), otherwise alphabetically on the titles.
| |||||||||||
Returns the Context of this Preference.
| |||||||||||
Returns the key of the dependency on this Preference.
| |||||||||||
Returns an SharedPreferences.Editor where this Preference can
save its value(s).
| |||||||||||
Return the Intent associated with this Preference.
| |||||||||||
Gets the key for this Preference, which is also the key used for storing
values into SharedPreferences.
| |||||||||||
Gets the layout resource that will be shown as the View for this Preference.
| |||||||||||
Returns the callback to be invoked when this Preference is changed by the
user (but before the internal state has been updated).
| |||||||||||
Returns the callback to be invoked when this Preference is clicked.
| |||||||||||
Gets the order of this Preference with respect to other Preference objects
on the same level.
| |||||||||||
Gets the PreferenceManager that manages this Preference object's tree.
| |||||||||||
Returns the SharedPreferences where this Preference can read its
value(s).
| |||||||||||
Checks whether this Preference should disable its view when it's action is disabled.
| |||||||||||
Returns the summary of this Preference.
| |||||||||||
Returns the title of this Preference.
| |||||||||||
Gets the View that will be shown in the PreferenceActivity.
| |||||||||||
Gets the layout resource for the controllable widget portion of this Preference.
| |||||||||||
Checks whether this Preference has a valid key.
| |||||||||||
Checks whether this Preference should be enabled in the list.
| |||||||||||
Checks whether this Preference is persistent.
| |||||||||||
Checks whether this Preference should be selectable in the list.
| |||||||||||
Notifies any listening dependents of a change that affects the
dependency.
| |||||||||||
Called when the dependency changes.
| |||||||||||
Restore this Preference hierarchy's previously saved state from the given container.
| |||||||||||
Store this Preference hierarchy's frozen state into the given container.
| |||||||||||
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.
| |||||||||||
Sets the key of a Preference that this Preference will depend on.
| |||||||||||
Sets whether this Preference is enabled.
| |||||||||||
Sets an Intent to be used for
startActivity(Intent) when this Preference is clicked.
| |||||||||||
Sets the key for this Preference, which is used as a key to the
SharedPreferences.
| |||||||||||
Sets the layout resource that is inflated as the View to be shown
for this Preference.
| |||||||||||
Sets the callback to be invoked when this Preference is changed by the
user (but before the internal state has been updated).
| |||||||||||
Sets the callback to be invoked when this Preference is clicked.
| |||||||||||
Sets the order of this Preference with respect to other
Preference objects on the same level.
| |||||||||||
Sets whether this Preference is persistent.
| |||||||||||
Sets whether this Preference is selectable.
| |||||||||||
Sets whether this Preference should disable its view when it gets
disabled.
| |||||||||||
Sets the summary for this Preference with a resource ID.
| |||||||||||
Sets the summary for this Preference with a CharSequence.
| |||||||||||
Sets the title for this Preference with a resource ID.
| |||||||||||
Sets the title for this Preference with a CharSequence.
| |||||||||||
Sets The layout for the controllable widget portion of this Preference.
| |||||||||||
Returns whether the Preference should commit its saved value(s) in
getEditor().
| |||||||||||
Checks whether this preference's dependents should currently be
disabled.
| |||||||||||
Returns a string containing a concise, human-readable description of this
object.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Call this method after the user changes the preference, but before the
internal state is set.
| |||||||||||
Finds a Preference in this hierarchy (the whole thing,
even above/below your PreferenceScreen screen break) with the given
key.
| |||||||||||
Attempts to get a persisted boolean from the SharedPreferences.
| |||||||||||
Attempts to get a persisted float from the SharedPreferences.
| |||||||||||
Attempts to get a persisted int from the SharedPreferences.
| |||||||||||
Attempts to get a persisted long from the SharedPreferences.
| |||||||||||
Attempts to get a persisted String from the SharedPreferences.
| |||||||||||
Should be called when the data of this Preference has changed.
| |||||||||||
Should be called when a Preference has been
added/removed from this group, or the ordering should be
re-evaluated.
| |||||||||||
Called when the Preference hierarchy has been attached to the
PreferenceActivity.
| |||||||||||
Called when this Preference has been attached to a Preference hierarchy.
| |||||||||||
Binds the created View to the data for this Preference.
| |||||||||||
Processes a click on the preference.
| |||||||||||
Creates the View to be shown for this Preference in the
PreferenceActivity.
| |||||||||||
Called when a Preference is being inflated and the default value
attribute needs to be read.
| |||||||||||
Called when this Preference is being removed from the hierarchy.
| |||||||||||
Hook allowing a Preference to re-apply a representation of its internal
state that had previously been generated by 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.
| |||||||||||
Implement this to set the initial value of the Preference.
| |||||||||||
Attempts to persist a boolean to the SharedPreferences.
| |||||||||||
Attempts to persist a float to the SharedPreferences.
| |||||||||||
Attempts to persist an int to the SharedPreferences.
| |||||||||||
Attempts to persist a long to the SharedPreferences.
| |||||||||||
Attempts to persist a String to the SharedPreferences.
| |||||||||||
Checks whether, at the given time this method is called,
this Preference should store/restore its value(s) into the
SharedPreferences.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
| |||||||||||
From interface java.lang.Comparable
|
The default value for the preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage.
May be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
May be an integer value, such as "100
".
May be a boolean value, either "true
" or "false
".
May be a floating point value, such as "1.2
".
This corresponds to the global attribute resource symbol defaultValue.
The key of another Preference that this Preference will depend on. If the other Preference is not set or is off, this Preference will be disabled.
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 dependency.
Whether the Preference is enabled.
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 enabled.
The key to store the Preference value.
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 key.
The layout for the Preference in a PreferenceActivity screen. This should rarely need to be changed, look at widgetLayout instead.
Must be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
This corresponds to the global attribute resource symbol layout.
The order for the Preference (lower values are to be ordered first). If this is not specified, the default orderin will be alphabetic.
Must be an integer value, such as "100
".
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 order.
Whether the Preference stores its value to the shared preferences.
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 persistent.
Whether the Preference is selectable.
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 selectable.
Whether the view of this Preference should be disabled when this Preference is disabled.
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 shouldDisableView.
The summary for the Preference in a PreferenceActivity screen.
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 summary.
The title for the Preference in a PreferenceActivity screen.
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 title.
The layout for the controllable widget portion of a Preference. This is inflated into the layout for a Preference and should be used more frequently than the layout attribute. For example, a checkbox preference would specify a custom layout (consisting of just the CheckBox) here.
Must be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
This corresponds to the global attribute resource symbol widgetLayout.
Specify for setOrder(int) if a specific order is not required.
Perform inflation from XML and apply a class-specific base style. This
constructor of Preference 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 defStyle.
This allows the theme's checkbox preference style to modify all of the base
preference attributes as well as the CheckBoxPreference class's
attributes.
context | The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc. |
---|---|
attrs | The attributes of the XML tag that is inflating the preference. |
defStyle | The default style to apply to this preference. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource. |
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.
context | The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc. |
---|---|
attrs | The attributes of the XML tag that is inflating the preference. |
Constructor to create a Preference.
context | The Context in which to store Preference values. |
---|
Compares Preference objects based on order (if set), otherwise alphabetically on the titles.
another | The Preference to compare to this one. |
---|
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 PreferenceActivity). This Context will be used to save the Preference values.
Returns the key of the dependency on this Preference.
Returns an SharedPreferences.Editor where this Preference can save its value(s). Usually it's easier to use one of the helper save methods: persistBoolean(boolean), persistFloat(float), persistInt(int), persistLong(long), persistString(String). To read values, see getSharedPreferences(). If shouldCommit() returns true, it is this Preference's responsibility to commit.
In some cases, writes to this will not be committed right away and hence not show up in the SharedPreferences, this is intended behavior to improve performance.
Return the Intent associated with this Preference.
Gets the key for this Preference, which is also the key used for storing values into SharedPreferences.
Gets the layout resource that will be shown as the View for this Preference.
Returns the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).
Returns the callback to be invoked when this Preference is clicked.
Gets the order of this Preference with respect to other Preference objects on the same level.
Gets the PreferenceManager that manages this Preference object's tree.
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). To save values, see getEditor().
In some cases, writes to the getEditor() will not be committed right away and hence not show up in the returned SharedPreferences, this is intended behavior to improve performance.
Checks whether this Preference should disable its view when it's action is disabled.
Gets the View that will be shown in the PreferenceActivity.
convertView | The old View to reuse, if possible. Note: You should check that this View is non-null and of an appropriate type before using. If it is not possible to convert this View to display the correct data, this method can create a new View. |
---|---|
parent | The parent that this View will eventually be attached to. |
Gets the layout resource for the controllable widget portion of this Preference.
Checks whether this Preference has a valid key.
Checks whether this Preference should be enabled in the list.
Checks whether this Preference is persistent. If it is, it stores its value(s) into the persistent SharedPreferences storage.
Checks whether this Preference should be selectable in the list.
Notifies any listening dependents of a change that affects the dependency.
disableDependents | Whether this Preference should disable its dependents. |
---|
Called when the dependency changes.
dependency | The Preference that this Preference depends on. |
---|---|
disableDependent | Set true to disable this Preference. |
Restore this Preference hierarchy's previously saved state from the given container.
container | The Bundle that holds the previously saved state. |
---|
Store this Preference hierarchy's frozen state into the given container.
container | The Bundle in which to save the instance of this Preference. |
---|
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.
defaultValue | The default value. |
---|
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.
dependencyKey | The key of the Preference that this depends on. |
---|
Sets whether this Preference is enabled. If disabled, it will not handle clicks.
enabled | Set true to enable it. |
---|
Sets an Intent to be used for startActivity(Intent) when this Preference is clicked.
intent | The intent associated with this Preference. |
---|
Sets the key for this Preference, which is used as a key to the SharedPreferences. This should be unique for the package.
key | The key for the preference. |
---|
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 widget_frame to be the parent of the specific widget for this Preference. It should similarly contain title and summary.
layoutResId | The layout resource ID to be inflated and returned as a View. |
---|
Sets the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).
onPreferenceChangeListener | The callback to be invoked. |
---|
Sets the callback to be invoked when this Preference is clicked.
onPreferenceClickListener | The callback to be invoked. |
---|
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 setOrderingAsAdded(boolean) can be used to order Preference objects based on the order they appear in the XML.
order | The order for this Preference. A lower value will be shown first. Use DEFAULT_ORDER to sort alphabetically or allow ordering from XML. |
---|
Sets whether this Preference is persistent. When persistent, it stores its value(s) into the persistent SharedPreferences storage.
persistent | Set true if it should store its value(s) into the SharedPreferences. |
---|
Sets whether this Preference is selectable.
selectable | Set true to make it selectable. |
---|
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.
shouldDisableView | Set true if this preference should disable its view when the preference is disabled. |
---|
Sets the summary for this Preference with a resource ID.
summaryResId | The summary as a resource. |
---|
Sets the summary for this Preference with a CharSequence.
summary | The summary for the preference. |
---|
Sets the title for this Preference with a resource ID.
titleResId | The title as a resource ID. |
---|
Sets the title for this Preference with a CharSequence. This title will be placed into the ID title within the View created by onCreateView(ViewGroup).
title | The title for this Preference. |
---|
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.
widgetLayoutResId | The layout resource ID to be inflated into the main layout. |
---|
Returns whether the Preference should commit its saved value(s) in getEditor(). This may return false in situations where batch committing is being done (by the manager) to improve performance.
Checks whether this preference's dependents should currently be disabled.
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation simply concatenates the class name, the '@' sign and a hexadecimal representation of the object's hashCode(), that is, it is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
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.
newValue | The new value of this Preference. |
---|
Finds a Preference in this hierarchy (the whole thing, even above/below your PreferenceScreen screen break) with the given key.
This only functions after we have been attached to a hierarchy.
key | The key of the Preference to find. |
---|
Attempts to get a persisted boolean from the SharedPreferences.
defaultReturnValue | The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
---|
Attempts to get a persisted float from the SharedPreferences.
defaultReturnValue | The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
---|
Attempts to get a persisted int from the SharedPreferences.
defaultReturnValue | The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
---|
Attempts to get a persisted long from the SharedPreferences.
defaultReturnValue | The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
---|
Attempts to get a persisted String from the SharedPreferences.
This will check if this Preference is persistent, get the SharedPreferences from the PreferenceManager, and get the value.
defaultReturnValue | The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences. |
---|
Should be called when the data of this Preference has changed.
Should be called when a Preference has been added/removed from this group, or the ordering should be re-evaluated.
Called when the Preference hierarchy has been attached to the PreferenceActivity. This can also be called when this Preference has been attached to a group that was already attached to the PreferenceActivity.
Called when this Preference has been attached to a Preference hierarchy. Make sure to call the super implementation.
preferenceManager | The PreferenceManager of the hierarchy. |
---|
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.
view | The View that shows this Preference. |
---|
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.
Creates the View to be shown for this Preference in the PreferenceActivity. The default behavior is to inflate the main layout of this Preference (see setLayoutResource(int). If changing this behavior, please specify a ViewGroup with ID widget_frame.
Make sure to call through to the superclass's implementation.
parent | The parent that this View will eventually be attached to. |
---|
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 getString(int).
a | The set of attributes. |
---|---|
index | The index of the default value attribute. |
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.
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.
state | The saved state that had previously been returned by 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. This state should only contain information that is not persistent or can be reconstructed later.
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).
This may not always be called. One example is if it should not persist but there is no default value given.
restorePersistedValue | True to restore the persisted value; false to use the given defaultValue. |
---|---|
defaultValue | The default value for this Preference. Only use this if restorePersistedValue is false. |
Attempts to persist a boolean to the SharedPreferences.
value | The value to persist. |
---|
Attempts to persist a float to the SharedPreferences.
value | The value to persist. |
---|
Attempts to persist an int to the SharedPreferences.
value | The value to persist. |
---|
Attempts to persist a long to the SharedPreferences.
value | The value to persist. |
---|
Attempts to persist a String to the SharedPreferences.
This will check if this Preference is persistent, get an editor from the PreferenceManager, put in the string, and check if we should commit (and commit if so).
value | The value to persist. |
---|
Checks whether, at the given time this method is called, this Preference should store/restore its value(s) into the SharedPreferences. This, at minimum, checks whether this Preference is persistent and it currently has a key. Before you save/restore from the SharedPreferences, check this first.