Android
android.preference
public class

android.preference.Preference

java.lang.Object
android.preference.Preference Comparable<T>

The Preference class represents the basic preference UI building block that is 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 tag name 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  
Preference.OnPreferenceChangeListener Interface definition for a callback to be invoked when this Preference's value 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. 
Known Direct Subclasses
Known Indirect Subclasses

Summary

XML Attributes

Attribute name Related methods  
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

      Value  
int  DEFAULT_ORDER  Specify for setOrder(int) if a specific order is not required.  2147483647  0x7fffffff 

Public Constructors

            Preference(Context context, AttributeSet attrs, int defStyle)
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

          int  compareTo(Preference another)
Compares preferences based on order (if set), otherwise alphabetically on title.
          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).
          Intent  getIntent()
Return the Intent associated with this preference.
          String  getKey()
Gets the key for the preference, which is also the key used for storing values into SharedPreferences.
          int  getLayoutResource()
Gets the layout resource that will be shown as the View for this preference.
          Preference.OnPreferenceChangeListener  getOnPreferenceChangeListener()
Gets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).
          Preference.OnPreferenceClickListener  getOnPreferenceClickListener()
Gets the callback to be invoked when this preference is clicked.
          int  getOrder()
Gets the order of this Preference.
          PreferenceManager  getPreferenceManager()
Gets the PreferenceManager that manages this preference's tree.
          SharedPreferences  getSharedPreferences()
Returns the SharedPreferences where this preference can read its value(s).
          boolean  getShouldDisableView()
          CharSequence  getSummary()
Returns the summary of the preference.
          CharSequence  getTitle()
Returns the title of the 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 a preference.
          boolean  hasKey()
Returns whether this Preference has a valid key.
          boolean  isEnabled()
Whether this Preference should be enabled in the list.
          boolean  isPersistent()
Returns whether this Preference is persistent.
          boolean  isSelectable()
Whether this Preference should be selectable in the list.
          void  notifyDependencyChange(boolean disableDependents)
Notifies any listening dependents of a change that affects the dependency.
          void  onDependencyChanged(Preference dependency, boolean disableDependent)
Called when the dependency changes.
          void  restoreHierarchyState(Bundle container)
Restore this preference hierarchy's frozen 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 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.
          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  setIntent(Intent intent)
Sets an Intent to be used for startActivity(Intent) when the preference is clicked.
          void  setKey(String key)
Sets the key for the 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 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(int summaryResId)
          void  setSummary(CharSequence summary)
Sets the summary for the preference.
          void  setTitle(int titleResId)
          void  setTitle(CharSequence title)
Sets the title for the preference.
          void  setWidgetLayoutResource(int widgetLayoutResId)
Sets The layout for the controllable widget portion of a preference.
          boolean  shouldCommit()
Returns whether the Preference should commit its saved value(s) in getEditor().
          boolean  shouldDisableDependents()
Should return whether this preference's dependents should currently be disabled.
          String  toString()
Returns a string containing a concise, human-readable description of the receiver.

Protected Methods

          boolean  callChangeListener(Object newValue)
Call this method after the user changes the preference, but before the internal state is set.
          Preference  findPreferenceInHierarchy(String key)
Find 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 this is a group 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 the 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.
          Object  onGetDefaultValue(TypedArray a, int index)
Called when 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()
Convenience method of whether at the given time this method is called, the Preference should store/restore its value(s) into the SharedPreferences.
Methods inherited from class java.lang.Object
Methods inherited from interface java.lang.Comparable

Details

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.

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.

Related Methods

android:dependency

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.

Related Methods

android:enabled

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.

Related Methods

android:key

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.

Related Methods

android:layout

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.

Related Methods

android:order

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.

Related Methods

android:persistent

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.

Related Methods

android:selectable

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.

Related Methods

android:shouldDisableView

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.

Related Methods

android:summary

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.

Related Methods

android:title

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.

Related Methods

android:widgetLayout

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.

Related Methods

Constants

public static final int DEFAULT_ORDER

Specify for setOrder(int) if a specific order is not required.
Constant Value: 2147483647 (0x7fffffff)

Public Constructors

public Preference(Context context, AttributeSet attrs, int defStyle)

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's constructor would call this version of the super class constructor and supply 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.

Parameters

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.

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

public Preference(Context context)

Constructor to create a Preference.

Parameters

context The context to store preference values.

Public Methods

public int compareTo(Preference another)

Compares preferences based on order (if set), otherwise alphabetically on title.

public 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 PreferenceActivity). This context will be used to save the preference valus.

Returns

  • The context of this preference.

public String getDependency()

Returns the key of the dependency on this preference.

Returns

  • The key of the dependency.

public SharedPreferences.Editor getEditor()

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 shared preferences, this is intended behavior to improve performance.

Returns

  • A SharedPreferences.Editor where this preference saves its value(s), or null if it isn't attached to a preference hierarchy.

public Intent getIntent()

Return the Intent associated with this preference.

Returns

public String getKey()

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

Returns

  • The key.

public int getLayoutResource()

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

Returns

  • The layout resource ID.

public Preference.OnPreferenceChangeListener getOnPreferenceChangeListener()

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

public Preference.OnPreferenceClickListener getOnPreferenceClickListener()

Gets the callback to be invoked when this preference is clicked.

Returns

  • The callback to be invoked.

public int getOrder()

Gets the order of this Preference.

Returns

See Also

public PreferenceManager getPreferenceManager()

Gets the PreferenceManager that manages this preference's tree.

Returns

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

Returns

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

See Also

public boolean getShouldDisableView()

Returns

  • Whether this preference should disable its view when it is disabled.

public CharSequence getSummary()

Returns the summary of the preference.

Returns

  • The summary.

public CharSequence getTitle()

Returns the title of the preference.

Returns

  • The title.

public View getView(View convertView, ViewGroup parent)

Gets the View that will be shown in the PreferenceActivity.

Parameters

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.

Returns

  • Returns the same Preference object, for chaining multiple calls into a single statement.

public int getWidgetLayoutResource()

Gets the layout resource for the controllable widget portion of a preference.

Returns

  • The layout resource ID.

public boolean hasKey()

Returns whether this Preference has a valid key.

Returns

  • Whether the key exists and is not a blank string.

public boolean isEnabled()

Whether this Preference should be enabled in the list.

Returns

  • Whether this preference is enabled.

public boolean isPersistent()

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

Returns

  • Whether this is persistent.

public boolean isSelectable()

Whether this Preference should be selectable in the list.

Returns

  • Whether this preference is selectable.

public void notifyDependencyChange(boolean disableDependents)

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

Parameters

disableDependents Whether this Preference should disable its dependents.

public void onDependencyChanged(Preference dependency, boolean disableDependent)

Called when the dependency changes.

Parameters

dependency The preference that this preference depends on.
disableDependent Whether to disable this preference.

public void restoreHierarchyState(Bundle container)

Restore this preference hierarchy's frozen state from the given container.

Parameters

container The Bundle which holds previously frozen icicles.

public void saveHierarchyState(Bundle container)

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

Parameters

container The Bundle in which to save the preference's icicles.

public void setDefaultValue(Object defaultValue)

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

Parameters

defaultValue The default value.

public 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 The key of the Preference that this depends on.

public void setEnabled(boolean enabled)

Sets whether this preference is enabled. If disabled, the preference will not handle clicks.

Parameters

enabled Whether the preference is enabled.

public void setIntent(Intent intent)

Sets an Intent to be used for startActivity(Intent) when the preference is clicked.

Parameters

intent The intent associated with the preference.

public void setKey(String key)

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

Parameters

key The key for the preference.

See Also

public 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 preferences 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.

Parameters

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

public 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 The callback to be invoked.

public void setOnPreferenceClickListener(Preference.OnPreferenceClickListener onPreferenceClickListener)

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

Parameters

onPreferenceClickListener The callback to be invoked.

public void setOrder(int order)

Sets the order of this Preference with respect to other Preference on the same level. If this is not specified, the default behavior is to sort alphabetically. The setOrderingAsAdded(boolean) can be used to order preferences based on the order they appear in the XML.

Parameters

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

public void setPersistent(boolean persistent)

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

Parameters

persistent Whether it should store its value(s) into the SharedPreferences.

public void setSelectable(boolean selectable)

Sets whether this preference is selectable.

Parameters

selectable Whether the preference is selectable.

public 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 Whether this preference should disable its view when the preference is disabled.

public void setSummary(int summaryResId)

Parameters

summaryResId The summary as a resource.

public void setSummary(CharSequence summary)

Sets the summary for the preference. This summary will be placed into the ID summary within the View created by onCreateView(ViewGroup).

Parameters

summary The summary of the preference.

public void setTitle(int titleResId)

Parameters

titleResId The title as a resource ID.

public void setTitle(CharSequence title)

Sets the title for the preference. This title will be placed into the ID title within the View created by onCreateView(ViewGroup).

Parameters

title The title of the preference.

public void setWidgetLayoutResource(int widgetLayoutResId)

Sets The layout for the controllable widget portion of a preference. This is inflated into the main layout. For example, a checkbox preference would specify a custom layout (consisting of just the CheckBox) here, instead of creating its own main layout.

Parameters

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

public boolean shouldCommit()

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.

Returns

  • Whether the Preference should commit its saved value(s).

See Also

public boolean shouldDisableDependents()

Should return whether this preference's dependents should currently be disabled.

Returns

  • True if the dependents should be disabled, otherwise false.

public String toString()

Returns a string containing a concise, human-readable description of the receiver.

Returns

  • String a printable representation for the receiver.

Protected Methods

protected 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 The new value of the preference.

Returns

  • Whether or not the user value should be set as the preference value (and persisted).

protected Preference findPreferenceInHierarchy(String key)

Find 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 The key of the Preference to find.

Returns

  • The Preference object of a preference with the given key.

protected boolean getPersistedBoolean(boolean defaultReturnValue)

Attempts to get a persisted boolean from the SharedPreferences.

Parameters

defaultReturnValue The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.

Returns

  • The value from the shared preferences or the default return value.

protected float getPersistedFloat(float defaultReturnValue)

Attempts to get a persisted float from the SharedPreferences.

Parameters

defaultReturnValue The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.

Returns

  • The value from the shared preferences or the default return value.

protected int getPersistedInt(int defaultReturnValue)

Attempts to get a persisted int from the SharedPreferences.

Parameters

defaultReturnValue The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.

Returns

  • The value from the shared preferences or the default return value.

protected long getPersistedLong(long defaultReturnValue)

Attempts to get a persisted long from the SharedPreferences.

Parameters

defaultReturnValue The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.

Returns

  • The value from the shared preferences or the default return value.

protected String getPersistedString(String defaultReturnValue)

Attempts to get a persisted String from the SharedPreferences.

This will check if the Preference is persistent, get the shared preferences from the preference manager, get the value.

Parameters

defaultReturnValue The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences.

Returns

  • The value from the shared preferences or the default return value.

protected void notifyChanged()

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

protected void notifyHierarchyChanged()

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

protected void onAttachedToActivity()

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.

protected void onAttachedToHierarchy(PreferenceManager preferenceManager)

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

Parameters

preferenceManager The preference manager of the hierarchy.

protected void onBindView(View view)

Binds the created View to the data for the 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.

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

protected View onCreateView(ViewGroup parent)

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.

Parameters

parent The parent that this view will eventually be attached to.

Returns

  • The View that displays this preference.

See Also

protected Object onGetDefaultValue(TypedArray a, int index)

Called when 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 The set of attributes.
index The index of the default value attribute.

Returns

  • The default value of this preference type.

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

protected 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 icicle.

Parameters

state The frozen state that had previously been returned by onSaveInstanceState().

protected 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

  • Returns a Parcelable object containing the preference's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.

protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue)

Implement this to set the initial value of the Preference. If the restoreValue flag is true, you should restore the value from the shared preferences. If false, you should set (and possibly store to shared preferences if shouldPersist()) to defaultValue.

This may not always be called. One example is if it should not persist but there is no default value given.

Parameters

restorePersistedValue Whether to restore the persisted value (true), or use the given default value (false).
defaultValue The default value. Only use if restoreValue is false.

protected boolean persistBoolean(boolean value)

Attempts to persist a boolean to the SharedPreferences.

Parameters

value The value to persist.

Returns

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

protected boolean persistFloat(float value)

Attempts to persist a float to the SharedPreferences.

Parameters

value The value to persist.

Returns

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

protected boolean persistInt(int value)

Attempts to persist an int to the SharedPreferences.

Parameters

value The value to persist.

Returns

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

protected boolean persistLong(long value)

Attempts to persist a long to the SharedPreferences.

Parameters

value The value to persist.

Returns

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

protected boolean persistString(String value)

Attempts to persist a String to the SharedPreferences.

This will check if the Preference is persistent, get an editor from the preference manager, put the string, check if we should commit (and commit if so).

Parameters

value The value to persist.

Returns

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

protected boolean shouldPersist()

Convenience method of whether at the given time this method is called, the Preference should store/restore its value(s) into the SharedPreferences. This, at minimum, checks whether the Preference is persistent and it currently has a key. Before you save/restore from the SharedPreferences, check this first.

Returns

  • Whether to persist the value.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:48