Preference


public class Preference
extends Object implements Comparable<Preference>

java.lang.Object
   ↳ android.preference.Preference


This class was deprecated in API level 29.
Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.

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.

Developer Guides

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

Summary

Nested classes

class Preference.BaseSavedState

This class was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. 

interface Preference.OnPreferenceChangeListener

This interface was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. 

interface Preference.OnPreferenceClickListener

This interface was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. 

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:fragment When used inside of a modern PreferenceActivity, this declares a new PreferenceFragment to be shown when the user selects this item. 
android:icon The optional icon for the preference. 
android:iconSpaceReserved Whether the space for the preference icon view will be reserved. 
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 storage. 
android:recycleEnabled Whether the preference has enabled to have its view recycled when used in the list view. 
android:selectable Whether the Preference is selectable. 
android:shouldDisableView Whether the view of this Preference should be disabled when this Preference is disabled. 
android:singleLineTitle Whether to use single line for the preference title text. 
android:summary The summary for the Preference. 
android:title The title for the Preference. 
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

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

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.

SharedPreferences.Editor getEditor()

Returns an SharedPreferences.Editor where this Preference can save its value(s).

Bundle getExtras()

Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one.

String getFragment()

Return the fragment class name associated with this Preference.

Drawable getIcon()

Returns the icon of this Preference.

Intent getIntent()

Return the Intent associated with this Preference.

String getKey()

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

int getLayoutResource()

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

Preference.OnPreferenceChangeListener getOnPreferenceChangeListener()

Returns the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).

Preference.OnPreferenceClickListener getOnPreferenceClickListener()

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

int getOrder()

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

PreferenceGroup getParent()

Returns the PreferenceGroup which is this Preference assigned to or null if this preference is not assigned to any group or is a root Preference.

Set<String> getPersistedStringSet(Set<String> defaultReturnValue)

Attempts to get a persisted set of Strings if this Preference is persistent.

PreferenceDataStore getPreferenceDataStore()

Returns PreferenceDataStore used by this Preference.

PreferenceManager getPreferenceManager()

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

SharedPreferences getSharedPreferences()

Returns the SharedPreferences where this Preference can read its value(s).

boolean getShouldDisableView()

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

CharSequence getSummary()

Returns the summary of this Preference.

CharSequence getTitle()

Returns the title of this Preference.

int getTitleRes()

Returns the title resource ID of this Preference.

View getView(View convertView, ViewGroup parent)

Gets the View that will be shown in the PreferenceActivity.

int getWidgetLayoutResource()

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

boolean hasKey()

Checks whether this Preference has a valid key.

boolean isEnabled()

Checks whether this Preference should be enabled in the list.

boolean isIconSpaceReserved()

Gets whether the space this preference icon view is reserved.

boolean isPersistent()

Checks whether this Preference is persistent.

boolean isRecycleEnabled()

Checks whether this Preference has enabled to have its view recycled when used in the list view.

boolean isSelectable()

Checks whether this Preference should be selectable in the list.

boolean isSingleLineTitle()

Gets whether the title of this preference is constrained to a single line.

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

boolean persistStringSet(Set<String> values)

Attempts to persist a set of Strings if this Preference is persistent.

void restoreHierarchyState(Bundle container)

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

void saveHierarchyState(Bundle container)

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

void setDefaultValue(Object defaultValue)

Sets the default value for this Preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage.

void setDependency(String dependencyKey)

Sets the key of a Preference that this Preference will depend on.

void setEnabled(boolean enabled)

Sets whether this Preference is enabled.

void setFragment(String fragment)

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

void setIcon(Drawable icon)

Sets the icon for this Preference with a Drawable.

void setIcon(int iconResId)

Sets the icon for this Preference with a resource ID.

void setIconSpaceReserved(boolean iconSpaceReserved)

Sets whether to reserve the space of this Preference icon view when no icon is provided.

void setIntent(Intent intent)

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

void setKey(String key)

Sets the key for this Preference, which is used as a key to the SharedPreferences or PreferenceDataStore.

void setLayoutResource(int layoutResId)

Sets the layout resource that is inflated as the View to be shown for this Preference.

void setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener)

Sets the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).

void setOnPreferenceClickListener(Preference.OnPreferenceClickListener onPreferenceClickListener)

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

void setOrder(int order)

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

void setPersistent(boolean persistent)

Sets whether this Preference is persistent.

void setPreferenceDataStore(PreferenceDataStore dataStore)

Sets a PreferenceDataStore to be used by this Preference instead of using SharedPreferences.

void setRecycleEnabled(boolean enabled)

Sets whether this Preference has enabled to have its view recycled when used in the list view.

void setSelectable(boolean selectable)

Sets whether this Preference is selectable.

void setShouldDisableView(boolean shouldDisableView)

Sets whether this Preference should disable its view when it gets disabled.

void setSingleLineTitle(boolean singleLineTitle)

Sets whether to constrain the title of this Preference to a single line instead of letting it wrap onto multiple lines.

void setSummary(CharSequence summary)

Sets the summary for this Preference with a CharSequence.

void setSummary(int summaryResId)

Sets the summary for this Preference with a resource ID.

void setTitle(CharSequence title)

Sets the title for this Preference with a CharSequence.

void setTitle(int titleResId)

Sets the title for this Preference with a resource ID.

void setWidgetLayoutResource(int widgetLayoutResId)

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

boolean shouldCommit()

Returns whether the Preference should commit its saved value(s) in getEditor().

boolean shouldDisableDependents()

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

String toString()

Returns a string representation of the object.

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)

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 if this Preference is persistent.

float getPersistedFloat(float defaultReturnValue)

Attempts to get a persisted float if this Preference is persistent.

int getPersistedInt(int defaultReturnValue)

Attempts to get a persisted int if this Preference is persistent.

long getPersistedLong(long defaultReturnValue)

Attempts to get a persisted long if this Preference is persistent.

String getPersistedString(String defaultReturnValue)

Attempts to get a persisted String if this Preference is persistent.

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 onAttachedToActivity()

Called when the Preference hierarchy has been attached to the PreferenceActivity.

void onAttachedToHierarchy(PreferenceManager preferenceManager)

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

void onBindView(View view)

Binds the created View to the data for this Preference.

void onClick()

Processes a click on the preference.

View onCreateView(ViewGroup parent)

Creates the View to be shown for this Preference in the PreferenceActivity.

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 if this Preference is persistent.

boolean persistFloat(float value)

Attempts to persist a long if this Preference is persistent.

boolean persistInt(int value)

Attempts to persist an int if this Preference is persistent.

boolean persistLong(long value)

Attempts to persist a long if this Preference is persistent.

boolean persistString(String value)

Attempts to persist a String if this Preference is persistent.

boolean shouldPersist()

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

Inherited methods

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 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, such as "true" or "false".

May be a floating point value, such as "1.2".

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.

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

android:enabled

Whether the Preference is enabled.

May be a boolean value, such as "true" or "false".

android:fragment

When used inside of a modern PreferenceActivity, this declares a new PreferenceFragment to be shown when the user selects this item.

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

android:icon

The optional icon for the preference.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:iconSpaceReserved

Whether the space for the preference icon view will be reserved. By default, preference icon view visibility will be set to GONE when there is no icon provided, so the default value of this attribute is false.

May be a boolean value, such as "true" or "false".

android:key

The key to store the Preference value.

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

android:layout

The layout for the Preference in a PreferenceActivity screen. This should rarely need to be changed, look at widgetLayout instead.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:order

The order for the Preference (lower values are to be ordered first). If this is not specified, the default ordering will be alphabetic.

May be an integer value, such as "100".

android:persistent

Whether the Preference stores its value to the storage.

May be a boolean value, such as "true" or "false".

android:recycleEnabled

Whether the preference has enabled to have its view recycled when used in the list view. This is true by default.

May be a boolean value, such as "true" or "false".

android:selectable

Whether the Preference is selectable.

May be a boolean value, such as "true" or "false".

android:shouldDisableView

Whether the view of this Preference should be disabled when this Preference is disabled.

May be a boolean value, such as "true" or "false".

android:singleLineTitle

Whether to use single line for the preference title text. By default, preference title will be constrained to one line, so the default value of this attribute is true.

May be a boolean value, such as "true" or "false".

android:summary

The summary for the Preference. In API 25 and earlier, this value is read as a plain string with styling information stripped.

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

android:title

The title for the Preference. In API 25 and earlier, this value is read as a plain string with styling information stripped.

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

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Constants

DEFAULT_ORDER

Added in API level 1
public static final int DEFAULT_ORDER

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

Constant Value: 2147483647 (0x7fffffff)

Public constructors

Preference

Added in API level 21
public 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.

Preference

Added in API level 1
public 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.

Preference

Added in API level 1
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 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.

Preference

Added in API level 1
public Preference (Context context)

Constructor to create a Preference.

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

Public methods

compareTo

Added in API level 1
public 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

Added in API level 1
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 values.

Returns
Context The Context of this Preference.

getDependency

Added in API level 1
public String getDependency ()

Returns the key of the dependency on this Preference.

Returns
String The key of the dependency.

getEditor

Added in API level 1
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(java.lang.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.

Returns
SharedPreferences.Editor a SharedPreferences.Editor where this preference saves its value(s). If this preference isn't attached to a Preference hierarchy or if a PreferenceDataStore has been set, this method returns null.

getExtras

Added in API level 11
Deprecated in API level 29
public 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

Added in API level 11
Deprecated in API level 29
public 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

Added in API level 11
Deprecated in API level 29
public Drawable getIcon ()

Returns the icon of this Preference.

Returns
Drawable the icon

See also:

getIntent

Added in API level 1
public Intent getIntent ()

Return the Intent associated with this Preference.

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

getKey

Added in API level 1
public String getKey ()

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

Returns
String The key.

getLayoutResource

Added in API level 1
public int getLayoutResource ()

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

Returns
int The layout resource ID.

getOnPreferenceChangeListener

Added in API level 1
public 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

Added in API level 1
public Preference.OnPreferenceClickListener getOnPreferenceClickListener ()

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

Returns
Preference.OnPreferenceClickListener The callback to be invoked.

getOrder

Added in API level 1
public 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:

getParent

Added in API level 26
Deprecated in API level 29
public PreferenceGroup getParent ()

Returns the PreferenceGroup which is this Preference assigned to or null if this preference is not assigned to any group or is a root Preference.

Returns
PreferenceGroup the parent PreferenceGroup or null if not attached to any

getPersistedStringSet

Added in API level 24
Deprecated in API level 29
public Set<String> getPersistedStringSet (Set<String> defaultReturnValue)

Attempts to get a persisted set of Strings if this Preference is persistent.

Parameters
defaultReturnValue Set: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
Set<String> The value from the data store or the default return value.

getPreferenceDataStore

Added in API level 26
Deprecated in API level 29
public PreferenceDataStore getPreferenceDataStore ()

Returns PreferenceDataStore used by this Preference. Returns null if SharedPreferences is used instead.

By default preferences always use SharedPreferences. To make this preference to use the PreferenceDataStore you need to assign your implementation to the Preference itself via setPreferenceDataStore(android.preference.PreferenceDataStore) or to its PreferenceManager via PreferenceManager#setPreferenceDataStore(PreferenceDataStore).

Returns
PreferenceDataStore The PreferenceDataStore used by this Preference or null if none.

getPreferenceManager

Added in API level 1
public PreferenceManager getPreferenceManager ()

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

Returns
PreferenceManager The PreferenceManager.

getSharedPreferences

Added in API level 1
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(java.lang.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
SharedPreferences the SharedPreferences where this Preference reads its value(s). If this preference isn't attached to a Preference hierarchy or if a PreferenceDataStore has been set, this method returns null.

getShouldDisableView

Added in API level 1
public 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

getSummary

Added in API level 1
public CharSequence getSummary ()

Returns the summary of this Preference.

Returns
CharSequence the summary

getTitle

Added in API level 1
public CharSequence getTitle ()

Returns the title of this Preference.

Returns
CharSequence the title

getTitleRes

Added in API level 14
Deprecated in API level 29
public int getTitleRes ()

Returns the title resource ID of this Preference. If the title did not come from a resource, 0 is returned.

Returns
int the title resource

See also:

getView

Added in API level 1
public View getView (View convertView, 
                ViewGroup parent)

Gets the View that will be shown in the PreferenceActivity.

Parameters
convertView View: 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 ViewGroup: The parent that this View will eventually be attached to.

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

getWidgetLayoutResource

Added in API level 1
public int getWidgetLayoutResource ()

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

Returns
int The layout resource ID.

hasKey

Added in API level 1
public 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

Added in API level 1
public boolean isEnabled ()

Checks whether this Preference should be enabled in the list.

Returns
boolean true if this Preference is enabled, false otherwise

isIconSpaceReserved

Added in API level 26
Deprecated in API level 29
public boolean isIconSpaceReserved ()

Gets whether the space this preference icon view is reserved.

Returns
boolean true if the space of this preference icon view is reserved

isPersistent

Added in API level 1
public boolean isPersistent ()

Checks whether this Preference is persistent. If it is, it stores its value(s) into the persistent SharedPreferences storage by default or into PreferenceDataStore if assigned.

Returns
boolean True if it is persistent.

isRecycleEnabled

Added in API level 26
Deprecated in API level 29
public boolean isRecycleEnabled ()

Checks whether this Preference has enabled to have its view recycled when used in the list view.

Returns
boolean true if this preference view should be recycled

isSelectable

Added in API level 1
public boolean isSelectable ()

Checks whether this Preference should be selectable in the list.

Returns
boolean true if it is selectable, false otherwise

isSingleLineTitle

Added in API level 26
Deprecated in API level 29
public boolean isSingleLineTitle ()

Gets whether the title of this preference is constrained to a single line.

Returns
boolean true if the title of this preference is constrained to a single line

notifyDependencyChange

Added in API level 1
public 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.

onDependencyChanged

Added in API level 1
public 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.

onParentChanged

Added in API level 19
Deprecated in API level 29
public 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

Added in API level 11
Deprecated in API level 29
public Bundle peekExtras ()

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

Returns
Bundle

persistStringSet

Added in API level 24
Deprecated in API level 29
public boolean persistStringSet (Set<String> values)

Attempts to persist a set of Strings if this Preference is persistent.

Parameters
values Set: The values 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.)

restoreHierarchyState

Added in API level 1
public 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.

saveHierarchyState

Added in API level 1
public 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.

setDefaultValue

Added in API level 1
public 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

Added in API level 1
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 String: The key of the Preference that this depends on.

setEnabled

Added in API level 1
public 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

Added in API level 11
Deprecated in API level 29
public 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

Added in API level 11
Deprecated in API level 29
public void setIcon (Drawable icon)

Sets the icon for this Preference with a Drawable. This icon will be placed into the ID R.id.icon within the View created by onCreateView(android.view.ViewGroup).

Parameters
icon Drawable: the optional icon for this Preference

setIcon

Added in API level 11
Deprecated in API level 29
public 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:

setIconSpaceReserved

Added in API level 26
Deprecated in API level 29
public void setIconSpaceReserved (boolean iconSpaceReserved)

Sets whether to reserve the space of this Preference icon view when no icon is provided.

Parameters
iconSpaceReserved boolean: set true if the space for the icon view should be reserved

setIntent

Added in API level 1
public void setIntent (Intent intent)

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

Parameters
intent Intent: The intent associated with this Preference.

setKey

Added in API level 1
public void setKey (String key)

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

Parameters
key String: The key for the preference.

setLayoutResource

Added in API level 1
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 Preference objects and only the widget layout needs to be changed.

This layout should contain a ViewGroup with ID R.id.widget_frame to be the parent of the specific widget for this Preference. It should similarly contain R.id.title and R.id.summary.

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

setOnPreferenceChangeListener

Added in API level 1
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 Preference.OnPreferenceChangeListener: The callback to be invoked.

setOnPreferenceClickListener

Added in API level 1
public 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

Added in API level 1
public 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 PreferenceGroup#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

setPersistent

Added in API level 1
public void setPersistent (boolean persistent)

Sets whether this Preference is persistent. When persistent, it stores its value(s) into the persistent SharedPreferences storage by default or into PreferenceDataStore if assigned.

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

setPreferenceDataStore

Added in API level 26
Deprecated in API level 29
public void setPreferenceDataStore (PreferenceDataStore dataStore)

Sets a PreferenceDataStore to be used by this Preference instead of using SharedPreferences.

The data store will remain assigned even if the Preference is moved around the preference hierarchy. It will also override a data store propagated from the PreferenceManager that owns this Preference.

Parameters
dataStore PreferenceDataStore: The PreferenceDataStore to be used by this Preference.

setRecycleEnabled

Added in API level 26
Deprecated in API level 29
public void setRecycleEnabled (boolean enabled)

Sets whether this Preference has enabled to have its view recycled when used in the list view. By default the recycling is enabled.

The value can be changed only before this preference is added to the preference hierarchy.

If view recycling is not allowed then each time the list view populates this preference the getView(android.view.View, android.view.ViewGroup) method receives a null convert view and needs to recreate the view. Otherwise view gets recycled and only onBindView(android.view.View) gets called.
If you override this method you must call through to the superclass implementation.

Parameters
enabled boolean: set true if this preference view should be recycled

setSelectable

Added in API level 1
public void setSelectable (boolean selectable)

Sets whether this Preference is selectable.

Parameters
selectable boolean: set true to make it selectable

setShouldDisableView

Added in API level 1
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 boolean: set true if this preference should disable its view when the preference is disabled

setSingleLineTitle

Added in API level 26
Deprecated in API level 29
public void setSingleLineTitle (boolean singleLineTitle)

Sets whether to constrain the title of this Preference to a single line instead of letting it wrap onto multiple lines.

Parameters
singleLineTitle boolean: set true if the title should be constrained to one line

setSummary

Added in API level 1
public void setSummary (CharSequence summary)

Sets the summary for this Preference with a CharSequence.

Parameters
summary CharSequence: the summary for the preference

setSummary

Added in API level 1
public void setSummary (int summaryResId)

Sets the summary for this Preference with a resource ID.

Parameters
summaryResId int: the summary as a resource

setTitle

Added in API level 1
public void setTitle (CharSequence title)

Sets the title for this Preference with a CharSequence. This title will be placed into the ID R.id.title within the View created by onCreateView(android.view.ViewGroup).

Parameters
title CharSequence: the title for this Preference

setTitle

Added in API level 1
public void setTitle (int titleResId)

Sets the title for this Preference with a resource ID.

Parameters
titleResId int: the title as a resource ID

setWidgetLayoutResource

Added in API level 1
public 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.

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

shouldCommit

Added in API level 1
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.

If this preference is using PreferenceDataStore this value is irrelevant.

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

See also:

shouldDisableDependents

Added in API level 1
public boolean shouldDisableDependents ()

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

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

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

Protected methods

callChangeListener

Added in API level 1
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 Object: The new value of this Preference.

Returns
boolean True if the user value should be set as the preference value (and persisted).

findPreferenceInHierarchy

Added in API level 1
protected 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

Added in API level 1
protected boolean getPersistedBoolean (boolean defaultReturnValue)

Attempts to get a persisted boolean if this Preference is persistent.

Parameters
defaultReturnValue boolean: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
boolean The value from the data store or the default return value.

getPersistedFloat

Added in API level 1
protected float getPersistedFloat (float defaultReturnValue)

Attempts to get a persisted float if this Preference is persistent.

Parameters
defaultReturnValue float: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
float The value from the data store or the default return value.

getPersistedInt

Added in API level 1
protected int getPersistedInt (int defaultReturnValue)

Attempts to get a persisted int if this Preference is persistent.

Parameters
defaultReturnValue int: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
int The value from the data store or the default return value.

getPersistedLong

Added in API level 1
protected long getPersistedLong (long defaultReturnValue)

Attempts to get a persisted long if this Preference is persistent.

Parameters
defaultReturnValue long: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
long The value from the data store or the default return value.

getPersistedString

Added in API level 1
protected String getPersistedString (String defaultReturnValue)

Attempts to get a persisted String if this Preference is persistent.

Parameters
defaultReturnValue String: The default value to return if either this Preference is not persistent or this Preference is not present.

Returns
String The value from the data store or the default return value.

notifyChanged

Added in API level 1
protected void notifyChanged ()

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

notifyHierarchyChanged

Added in API level 1
protected void notifyHierarchyChanged ()

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

onAttachedToActivity

Added in API level 1
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.

onAttachedToHierarchy

Added in API level 1
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 PreferenceManager: The PreferenceManager of the hierarchy.

onBindView

Added in API level 1
protected void onBindView (View view)

Binds the created View to the data for this Preference.

This is a good place to grab references to custom Views in the layout and set properties on them.

Make sure to call through to the superclass's implementation.
If you override this method you must call through to the superclass implementation.

Parameters
view View: The View that shows this Preference.

onClick

Added in API level 1
protected void onClick ()

Processes a click on the preference. This includes saving the value to the SharedPreferences. However, the overridden method should call callChangeListener(java.lang.Object) to make sure the client wants to update the preference's state with the new value.

onCreateView

Added in API level 1
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 R.id.widget_frame.

Make sure to call through to the superclass's implementation.
If you override this method you must call through to the superclass implementation.

Parameters
parent ViewGroup: The parent that this View will eventually be attached to.

Returns
View The View that displays this Preference.

See also:

onGetDefaultValue

Added in API level 1
protected 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 TypedArray#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

Added in API level 1
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.
If you override this method you must call through to the superclass implementation.

onRestoreInstanceState

Added in API level 1
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 state.

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

onSaveInstanceState

Added in API level 1
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
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.

onSetInitialValue

Added in API level 1
protected 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).

In case of using PreferenceDataStore, the restorePersistedValue is always true. But the default value (if provided) is set.

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

Added in API level 1
protected boolean persistBoolean (boolean value)

Attempts to persist a boolean if this Preference is persistent.

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

persistFloat

Added in API level 1
protected boolean persistFloat (float value)

Attempts to persist a long if this Preference is persistent.

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

persistInt

Added in API level 1
protected boolean persistInt (int value)

Attempts to persist an int if this Preference is persistent.

Parameters
value int: 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.)

persistLong

Added in API level 1
protected boolean persistLong (long value)

Attempts to persist a long if this Preference is persistent.

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

persistString

Added in API level 1
protected boolean persistString (String value)

Attempts to persist a String if this Preference is persistent.

Parameters
value String: 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.)

shouldPersist

Added in API level 1
protected boolean shouldPersist ()

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

Returns
boolean True if it should persist the value.