Most visited

Recently visited

Preference

public class Preference
extends Object implements Comparable<Preference>

java.lang.Object
   ↳ android.support.v7.preference.Preference
Known Direct Subclasses
Known Indirect Subclasses


Represents the basic Preference UI building block displayed by a PreferenceFragmentCompat in the form of a RecyclerView. This class provides data for the View to be displayed in the list 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.

Developer Guides

For information about building a settings UI with Preferences, read the Settings guide.

Summary

Nested classes

class Preference.BaseSavedState

A base class for managing the instance state of a Preference

interface 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. 

interface Preference.OnPreferenceClickListener

Interface definition for a callback to be invoked when a Preference is clicked. 

Constants

int DEFAULT_ORDER

Specify for setOrder(int) if a specific order is not required.

Public constructors

Preference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Perform inflation from XML and apply a class-specific base style.

Preference(Context context, AttributeSet attrs, int defStyleAttr)

Perform inflation from XML and apply a class-specific base style.

Preference(Context context, AttributeSet attrs)

Constructor that is called when inflating a Preference from XML.

Preference(Context context)

Constructor to create a Preference.

Public methods

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.

Context getContext()

Returns the Context of this Preference.

String getDependency()

Returns the key of the dependency on this Preference.

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.

final 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.

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.

final 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 isPersistent()

Checks whether this Preference is persistent.

boolean isSelectable()

Checks whether this Preference should be selectable in the list.

final boolean isVisible()

Checks whether this preference should be visible to the user in the list.

void notifyDependencyChange(boolean disableDependents)

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

void onAttached()

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

void onBindViewHolder(PreferenceViewHolder holder)

Binds the created View to the data for this Preference.

void onDependencyChanged(Preference dependency, boolean disableDependent)

Called when the dependency changes.

void onDetached()

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

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info)

Initializes an AccessibilityNodeInfo with information about the View for this Preference.

void onParentChanged(Preference parent, boolean disableChild)

Called when the implicit parent dependency changes.

Bundle peekExtras()

Return the extras Bundle object associated with this preference, returning null if there is not currently one.

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 setIntent(Intent intent)

Sets an Intent to be used for 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.

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 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 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 setViewId(int viewId)

Set the ID that will be assigned to the overall View representing this preference, once bound.

final void setVisible(boolean visible)

Sets whether this preference should be visible in the list.

void setWidgetLayoutResource(int widgetLayoutResId)

Sets the layout for the controllable widget portion of this Preference.

boolean shouldDisableDependents()

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

String toString()

Returns a string representation of the object.

Protected methods

Preference findPreferenceInHierarchy(String key)

Finds a Preference in this hierarchy (the whole thing, even above/below your PreferenceScreen screen break) with the given key.

boolean getPersistedBoolean(boolean defaultReturnValue)

Attempts to get a persisted boolean from the SharedPreferences.

float getPersistedFloat(float defaultReturnValue)

Attempts to get a persisted float from the SharedPreferences.

int getPersistedInt(int defaultReturnValue)

Attempts to get a persisted int from the SharedPreferences.

long getPersistedLong(long defaultReturnValue)

Attempts to get a persisted long from the SharedPreferences.

String getPersistedString(String defaultReturnValue)

Attempts to get a persisted String from the SharedPreferences.

void notifyChanged()

Should be called when the data of this Preference has changed.

void notifyHierarchyChanged()

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

void onAttachedToHierarchy(PreferenceManager preferenceManager)

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

void onClick()

Processes a click on the preference.

Object onGetDefaultValue(TypedArray a, int index)

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

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.

boolean persistBoolean(boolean value)

Attempts to persist a boolean to the SharedPreferences.

boolean persistFloat(float value)

Attempts to persist a float to the SharedPreferences.

boolean persistInt(int value)

Attempts to persist an int to the SharedPreferences.

boolean persistLong(long value)

Attempts to persist a long to the SharedPreferences.

boolean persistString(String value)

Attempts to persist a String to the SharedPreferences.

boolean shouldPersist()

Checks whether, at the given time this method is called, this Preference should store/restore its value(s) into the SharedPreferences.

Inherited methods

From class java.lang.Object
From interface java.lang.Comparable

Constants

DEFAULT_ORDER

int DEFAULT_ORDER

Specify for setOrder(int) if a specific order is not required.

Constant Value: 2147483647 (0x7fffffff)

Public constructors

Preference

Preference (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

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 defStyleAttr. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as the CheckBoxPreference class's attributes.

Parameters
context Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc.
attrs AttributeSet: The attributes of the XML tag that is inflating the preference.
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.

See also:

Preference

Preference (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

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 defStyleAttr. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as the CheckBoxPreference class's attributes.

Parameters
context Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc.
attrs AttributeSet: The attributes of the XML tag that is inflating the preference.
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.

See also:

Preference

Preference (Context context, 
                AttributeSet attrs)

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.

Parameters
context Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc.
attrs AttributeSet: The attributes of the XML tag that is inflating the preference.

See also:

Preference

Preference (Context context)

Constructor to create a Preference.

Parameters
context Context: The Context in which to store Preference values.

Public methods

callChangeListener

boolean callChangeListener (Object newValue)

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.

Parameters
newValue Object: The new value of this Preference.
Returns
boolean True if the user value should be set as the preference value (and persisted).

compareTo

int compareTo (Preference another)

Compares Preference objects based on order (if set), otherwise alphabetically on the titles.

Parameters
another Preference: The Preference to compare to this one.
Returns
int 0 if the same; less than 0 if this Preference sorts ahead of another; greater than 0 if this Preference sorts after another.

getContext

Context getContext ()

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 PreferenceFragmentCompat). This Context will be used to save the Preference values.

Returns
Context The Context of this Preference.

getDependency

String getDependency ()

Returns the key of the dependency on this Preference.

Returns
String The key of the dependency.

See also:

getExtras

Bundle getExtras ()

Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one. You can use this to get and set individual extra key/value pairs.

Returns
Bundle

getFragment

String getFragment ()

Return the fragment class name associated with this Preference.

Returns
String The fragment class name last set via setFragment(String) or XML.

getIcon

Drawable getIcon ()

Returns the icon of this Preference.

Returns
Drawable The icon.

See also:

getIntent

Intent getIntent ()

Return the Intent associated with this Preference.

Returns
Intent The Intent last set via setIntent(Intent) or XML.

getKey

String getKey ()

Gets the key for this Preference, which is also the key used for storing values into SharedPreferences.

Returns
String The key.

getLayoutResource

int getLayoutResource ()

Gets the layout resource that will be shown as the View for this Preference.

Returns
int The layout resource ID.

getOnPreferenceChangeListener

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).

Returns
Preference.OnPreferenceChangeListener The callback to be invoked.

getOnPreferenceClickListener

Preference.OnPreferenceClickListener getOnPreferenceClickListener ()

Returns the callback to be invoked when this Preference is clicked.

Returns
Preference.OnPreferenceClickListener The callback to be invoked.

getOrder

int getOrder ()

Gets the order of this Preference with respect to other Preference objects on the same level.

Returns
int The order of this Preference.

See also:

getPreferenceManager

PreferenceManager getPreferenceManager ()

Gets the PreferenceManager that manages this Preference object's tree.

Returns
PreferenceManager The PreferenceManager.

getSharedPreferences

SharedPreferences getSharedPreferences ()

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).

Returns
SharedPreferences The SharedPreferences where this Preference reads its value(s), or null if it isn't attached to a Preference hierarchy.

getShouldDisableView

boolean getShouldDisableView ()

Checks whether this Preference should disable its view when it's action is disabled.

Returns
boolean True if it should disable the view.

See also:

getSummary

CharSequence getSummary ()

Returns the summary of this Preference.

Returns
CharSequence The summary.

See also:

getTitle

CharSequence getTitle ()

Returns the title of this Preference.

Returns
CharSequence The title.

See also:

getWidgetLayoutResource

int getWidgetLayoutResource ()

Gets the layout resource for the controllable widget portion of this Preference.

Returns
int The layout resource ID.

hasKey

boolean hasKey ()

Checks whether this Preference has a valid key.

Returns
boolean True if the key exists and is not a blank string, false otherwise.

isEnabled

boolean isEnabled ()

Checks whether this Preference should be enabled in the list.

Returns
boolean True if this Preference is enabled, false otherwise.

isPersistent

boolean isPersistent ()

Checks whether this Preference is persistent. If it is, it stores its value(s) into the persistent SharedPreferences storage.

Returns
boolean True if it is persistent.

isSelectable

boolean isSelectable ()

Checks whether this Preference should be selectable in the list.

Returns
boolean True if it is selectable, false otherwise.

isVisible

boolean isVisible ()

Checks whether this preference should be visible to the user in the list.

Returns
boolean True if this preference should be displayed.

See also:

notifyDependencyChange

void notifyDependencyChange (boolean disableDependents)

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

Parameters
disableDependents boolean: Whether this Preference should disable its dependents.

onAttached

void onAttached ()

Called when the Preference hierarchy has been attached to the list of preferences. This can also be called when this Preference has been attached to a group that was already attached to the list of preferences.

onBindViewHolder

void onBindViewHolder (PreferenceViewHolder holder)

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.

onDependencyChanged

void onDependencyChanged (Preference dependency, 
                boolean disableDependent)

Called when the dependency changes.

Parameters
dependency Preference: The Preference that this Preference depends on.
disableDependent boolean: Set true to disable this Preference.

onDetached

void onDetached ()

Called when the Preference hierarchy has been detached from the list of preferences. This can also be called when this Preference has been removed from a group that was attached to the list of preferences.

onInitializeAccessibilityNodeInfo

void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfoCompat info)

Initializes an AccessibilityNodeInfo with information about the View for this Preference.

Parameters
info AccessibilityNodeInfoCompat

onParentChanged

void onParentChanged (Preference parent, 
                boolean disableChild)

Called when the implicit parent dependency changes.

Parameters
parent Preference: The Preference that this Preference depends on.
disableChild boolean: Set true to disable this Preference.

peekExtras

Bundle peekExtras ()

Return the extras Bundle object associated with this preference, returning null if there is not currently one.

Returns
Bundle

restoreHierarchyState

void restoreHierarchyState (Bundle container)

Restore this Preference hierarchy's previously saved state from the given container.

Parameters
container Bundle: The Bundle that holds the previously saved state.

See also:

saveHierarchyState

void saveHierarchyState (Bundle container)

Store this Preference hierarchy's frozen state into the given container.

Parameters
container Bundle: The Bundle in which to save the instance of this Preference.

See also:

setDefaultValue

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.

Parameters
defaultValue Object: The default value.

setDependency

void setDependency (String dependencyKey)

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.

Parameters
dependencyKey String: The key of the Preference that this depends on.

setEnabled

void setEnabled (boolean enabled)

Sets whether this Preference is enabled. If disabled, it will not handle clicks.

Parameters
enabled boolean: Set true to enable it.

setFragment

void setFragment (String fragment)

Sets the class name of a fragment to be shown when this Preference is clicked.

Parameters
fragment String: The class name of the fragment associated with this Preference.

setIcon

void setIcon (Drawable icon)

Sets the icon for this Preference with a Drawable. This icon will be placed into the ID icon within the View created by onBindViewHolder(PreferenceViewHolder).

Parameters
icon Drawable: The optional icon for this Preference.

setIcon

void setIcon (int iconResId)

Sets the icon for this Preference with a resource ID.

Parameters
iconResId int: The icon as a resource ID.

See also:

setIntent

void setIntent (Intent intent)

Sets an Intent to be used for startActivity(Intent) when this Preference is clicked.

Parameters
intent Intent: The intent associated with this Preference.

setKey

void setKey (String key)

Sets the key for this Preference, which is used as a key to the SharedPreferences. This should be unique for the package.

Parameters
key String: The key for the preference.

setLayoutResource

void setLayoutResource (int layoutResId)

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.

It is an error to change the layout after adding the preference to a PreferenceGroup

Parameters
layoutResId int: The layout resource ID to be inflated and returned as a View.

See also:

setOnPreferenceChangeListener

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).

Parameters
onPreferenceChangeListener Preference.OnPreferenceChangeListener: The callback to be invoked.

setOnPreferenceClickListener

void setOnPreferenceClickListener (Preference.OnPreferenceClickListener onPreferenceClickListener)

Sets the callback to be invoked when this Preference is clicked.

Parameters
onPreferenceClickListener Preference.OnPreferenceClickListener: The callback to be invoked.

setOrder

void setOrder (int order)

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.

Parameters
order int: The order for this Preference. A lower value will be shown first. Use DEFAULT_ORDER to sort alphabetically or allow ordering from XML.

See also:

setPersistent

void setPersistent (boolean persistent)

Sets whether this Preference is persistent. When persistent, it stores its value(s) into the persistent SharedPreferences storage.

Parameters
persistent boolean: Set true if it should store its value(s) into the SharedPreferences.

setSelectable

void setSelectable (boolean selectable)

Sets whether this Preference is selectable.

Parameters
selectable boolean: Set true to make it selectable.

setShouldDisableView

void setShouldDisableView (boolean shouldDisableView)

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.

Parameters
shouldDisableView boolean: Set true if this preference should disable its view when the preference is disabled.

setSummary

void setSummary (CharSequence summary)

Sets the summary for this Preference with a CharSequence.

Parameters
summary CharSequence: The summary for the preference.

setSummary

void setSummary (int summaryResId)

Sets the summary for this Preference with a resource ID.

Parameters
summaryResId int: The summary as a resource.

See also:

setTitle

void setTitle (CharSequence title)

Sets the title for this Preference with a CharSequence. This title will be placed into the ID title within the View bound by onBindViewHolder(PreferenceViewHolder).

Parameters
title CharSequence: The title for this Preference.

setTitle

void setTitle (int titleResId)

Sets the title for this Preference with a resource ID.

Parameters
titleResId int: The title as a resource ID.

See also:

setViewId

void setViewId (int viewId)

Set the ID that will be assigned to the overall View representing this preference, once bound.

Parameters
viewId int

See also:

setVisible

void setVisible (boolean visible)

Sets whether this preference should be visible in the list. If false, it is excluded from the adapter, but can still be retrieved using findPreference(CharSequence).

Parameters
visible boolean: Set false if this preference should be hidden from the list.

setWidgetLayoutResource

void setWidgetLayoutResource (int widgetLayoutResId)

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.

It is an error to change the layout after adding the preference to a PreferenceGroup

Parameters
widgetLayoutResId int: The layout resource ID to be inflated into the main layout.

See also:

shouldDisableDependents

boolean shouldDisableDependents ()

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

Returns
boolean True if the dependents should be disabled, otherwise false.

toString

String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

Protected methods

findPreferenceInHierarchy

Preference findPreferenceInHierarchy (String key)

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.

Parameters
key String: The key of the Preference to find.
Returns
Preference The Preference that uses the given key.

getPersistedBoolean

boolean getPersistedBoolean (boolean defaultReturnValue)

Attempts to get a persisted boolean from the SharedPreferences.

Parameters
defaultReturnValue boolean: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences.
Returns
boolean The value from the SharedPreferences or the default return value.

See also:

getPersistedFloat

float getPersistedFloat (float defaultReturnValue)

Attempts to get a persisted float from the SharedPreferences.

Parameters
defaultReturnValue float: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences.
Returns
float The value from the SharedPreferences or the default return value.

See also:

getPersistedInt

int getPersistedInt (int defaultReturnValue)

Attempts to get a persisted int from the SharedPreferences.

Parameters
defaultReturnValue int: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences.
Returns
int The value from the SharedPreferences or the default return value.

See also:

getPersistedLong

long getPersistedLong (long defaultReturnValue)

Attempts to get a persisted long from the SharedPreferences.

Parameters
defaultReturnValue long: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences.
Returns
long The value from the SharedPreferences or the default return value.

See also:

getPersistedString

String getPersistedString (String defaultReturnValue)

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.

Parameters
defaultReturnValue String: The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.
Returns
String The value from the SharedPreferences or the default return value.

See also:

notifyChanged

void notifyChanged ()

Should be called when the data of this Preference has changed.

notifyHierarchyChanged

void notifyHierarchyChanged ()

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

onAttachedToHierarchy

void onAttachedToHierarchy (PreferenceManager preferenceManager)

Called when this Preference has been attached to a Preference hierarchy. Make sure to call the super implementation.

Parameters
preferenceManager PreferenceManager: The PreferenceManager of the hierarchy.

onClick

void onClick ()

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.

onGetDefaultValue

Object onGetDefaultValue (TypedArray a, 
                int index)

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).

Parameters
a TypedArray: The set of attributes.
index int: The index of the default value attribute.
Returns
Object The default value of this preference type.

onPrepareForRemoval

void onPrepareForRemoval ()

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.

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

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.

Parameters
state Parcelable: The saved state that had previously been returned by onSaveInstanceState().

See also:

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. This state should only contain information that is not persistent or can be reconstructed later.

Returns
Parcelable A Parcelable object containing the current dynamic state of this Preference, or null if there is nothing interesting to save. The default implementation returns null.

See also:

onSetInitialValue

void onSetInitialValue (boolean restorePersistedValue, 
                Object defaultValue)

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.

Parameters
restorePersistedValue boolean: True to restore the persisted value; false to use the given defaultValue.
defaultValue Object: The default value for this Preference. Only use this if restorePersistedValue is false.

persistBoolean

boolean persistBoolean (boolean value)

Attempts to persist a boolean to the SharedPreferences.

Parameters
value boolean: The value to persist.
Returns
boolean True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.)

See also:

persistFloat

boolean persistFloat (float value)

Attempts to persist a float to the SharedPreferences.

Parameters
value float: The value to persist.
Returns
boolean True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.)

See also:

persistInt

boolean persistInt (int value)

Attempts to persist an int to the SharedPreferences.

Parameters
value int: The value to persist.
Returns
boolean True if the Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.)

See also:

persistLong

boolean persistLong (long value)

Attempts to persist a long to the SharedPreferences.

Parameters
value long: The value to persist.
Returns
boolean True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.)

See also:

persistString

boolean persistString (String value)

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).

Parameters
value String: The value to persist.
Returns
boolean True if the Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.)

See also:

shouldPersist

boolean shouldPersist ()

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.

Returns
boolean True if it should persist the value.

Hooray!