Preference

public class Preference
extends Object implements Comparable<Preference>

java.lang.Object
   ↳ androidx.preference.Preference


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 or PreferenceDataStore 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. 

interface Preference.SummaryProvider<T extends Preference>

Interface definition for a callback to be invoked when the summary of this Preference is requested (typically when this preference is added to the hierarchy or its value is updated). 

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 or PreferenceDataStore.

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.

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.

final SummaryProvider getSummaryProvider()

Returns the Preference.SummaryProvider used to configure 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 isIconSpaceReserved()

Returns whether the space of this preference icon view is reserved.

boolean isPersistent()

Checks whether this preference is persistent.

boolean isSelectable()

Checks whether this preference should be selectable in the list.

final boolean isShown()

Checks whether this preference is shown to the user in the hierarchy.

boolean isSingleLineTitle()

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

final boolean isVisible()

Checks whether this preference should be visible to the user.

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.

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

final void setSummaryProvider(SummaryProvider summaryProvider)

Set a Preference.SummaryProvider that will be invoked whenever the summary of this preference is requested.

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 to the user.

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

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 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 Integer 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 set of Strings 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 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(Object defaultValue)

Implement this to set the initial value of the preference.

void onSetInitialValue(boolean restorePersistedValue, Object defaultValue)

This method is deprecated. Use onSetInitialValue(Object) instead.

boolean persistBoolean(boolean value)

Attempts to persist a Boolean if this preference is persistent.

boolean persistFloat(float value)

Attempts to persist a Float if this preference is persistent.

boolean persistInt(int value)

Attempts to persist an Integer 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

Constants

DEFAULT_ORDER

public static final int DEFAULT_ORDER

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

Constant Value: 2147483647 (0x7fffffff)

Public constructors

Preference

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

Perform inflation from XML and apply a class-specific base style. This constructor 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

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

Perform inflation from XML and apply a class-specific base style. This constructor 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

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

public Preference (Context context)

Constructor to create a preference.

Parameters
context Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc.

Public methods

callChangeListener

public 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

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

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

Returns
Context The Context of this preference

getDependency

public String getDependency ()

Returns the key of the dependency on this preference.

Returns
String The key of the dependency

getExtras

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

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

public Drawable getIcon ()

Returns the icon of this preference.

Returns
Drawable The icon

See also:

getIntent

public Intent getIntent ()

Return the Intent associated with this preference.

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

getKey

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

public final int getLayoutResource ()

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

Returns
int The layout resource ID

getOnPreferenceChangeListener

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

public Preference.OnPreferenceClickListener getOnPreferenceClickListener ()

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

Returns
Preference.OnPreferenceClickListener The callback to be invoked

getOrder

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

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

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 storage or the default return value

getPreferenceDataStore

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(PreferenceDataStore) or to its PreferenceManager via PreferenceManager.setPreferenceDataStore(PreferenceDataStore).

Returns
PreferenceDataStore The PreferenceDataStore used by this preference or null if none

getPreferenceManager

public PreferenceManager getPreferenceManager ()

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

Returns
PreferenceManager The PreferenceManager

getSharedPreferences

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

Returns
SharedPreferences The SharedPreferences where this preference reads its value(s). If this preference is not attached to a preference hierarchy or if a PreferenceDataStore has been set, this method returns null.

getShouldDisableView

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

public CharSequence getSummary ()

Returns the summary of this preference. If a Preference.SummaryProvider has been set for this preference, it will be used to provide the summary returned by this method.

Returns
CharSequence The summary

getSummaryProvider

public final SummaryProvider getSummaryProvider ()

Returns the Preference.SummaryProvider used to configure the summary of this preference.

Returns
SummaryProvider The Preference.SummaryProvider used to configure the summary of this preference, or null if there is no SummaryProvider set

getTitle

public CharSequence getTitle ()

Returns the title of this preference.

Returns
CharSequence The title

getWidgetLayoutResource

public final int getWidgetLayoutResource ()

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

Returns
int The layout resource ID

hasKey

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

public boolean isEnabled ()

Checks whether this preference should be enabled in the list.

Returns
boolean true if this preference is enabled, false otherwise

isIconSpaceReserved

public boolean isIconSpaceReserved ()

Returns whether the space of this preference icon view is reserved.

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

isPersistent

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 persistent

isSelectable

public boolean isSelectable ()

Checks whether this preference should be selectable in the list.

Returns
boolean true if it is selectable, false otherwise

isShown

public final boolean isShown ()

Checks whether this preference is shown to the user in the hierarchy. For a preference to be shown in the hierarchy, it and all of its ancestors must be visible and attached to the root PreferenceScreen.

Returns
boolean true if this preference is shown to the user in the hierarchy

isSingleLineTitle

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

isVisible

public final boolean isVisible ()

Checks whether this preference should be visible to the user. If this preference is visible, but one or more of its ancestors are not visible, then this preference will not be shown until its ancestors are all visible.

Returns
boolean true if this preference should be displayed

notifyDependencyChange

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.

onAttached

public 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

public 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

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

onDetached

public 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

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfoCompat info)

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

Parameters
info AccessibilityNodeInfoCompat

onParentChanged

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

public Bundle peekExtras ()

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

Returns
Bundle

persistStringSet

public boolean persistStringSet (Set<String> values)

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

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
values Set: The values to persist

Returns
boolean true if the preference is persistent, false otherwise

restoreHierarchyState

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

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

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

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

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

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

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 onBindViewHolder(PreferenceViewHolder).

Parameters
icon Drawable: The optional icon for this preference

setIcon

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

public void setIconSpaceReserved (boolean iconSpaceReserved)

Sets whether to reserve the space of this preference icon view when no icon is provided. If set to true, the preference will be offset as if it would have the icon and thus aligned with other preferences having icons.

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

setIntent

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

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

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.

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

setOnPreferenceChangeListener

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

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

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

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

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

setSelectable

public void setSelectable (boolean selectable)

Sets whether this preference is selectable.

Parameters
selectable boolean: Set true to make it selectable

setShouldDisableView

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

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

public void setSummary (CharSequence summary)

Sets the summary for this preference with a CharSequence.

You can also use a Preference.SummaryProvider to dynamically configure the summary of this preference.

Parameters
summary CharSequence: The summary for the preference

Throws
IllegalStateException If a Preference.SummaryProvider has already been set.

setSummary

public void setSummary (int summaryResId)

Sets the summary for this preference with a resource ID.

You can also use a Preference.SummaryProvider to dynamically configure the summary of this preference.

Parameters
summaryResId int: The summary as a resource

setSummaryProvider

public final void setSummaryProvider (SummaryProvider summaryProvider)

Set a Preference.SummaryProvider that will be invoked whenever the summary of this preference is requested. Set null to remove the existing SummaryProvider.

Parameters
summaryProvider SummaryProvider: The Preference.SummaryProvider that will be invoked whenever the summary of this preference is requested

setTitle

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 bound by onBindViewHolder(PreferenceViewHolder).

Parameters
title CharSequence: The title for this preference

setTitle

public void setTitle (int titleResId)

Sets the title for this preference with a resource ID.

Parameters
titleResId int: The title as a resource ID

setViewId

public 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

public final void setVisible (boolean visible)

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

To show this preference to the user, its ancestors must also all be visible. If you make a PreferenceGroup invisible, none of its children will be shown to the user until the group is visible.

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

See also:

setWidgetLayoutResource

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.

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

shouldDisableDependents

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

public String toString ()

Returns
String

Protected methods

findPreferenceInHierarchy

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

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 in the SharedPreferences.

Returns
boolean The value from the storage or the default return value

getPersistedFloat

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

Returns
float The value from the storage or the default return value

getPersistedInt

protected int getPersistedInt (int defaultReturnValue)

Attempts to get a persisted Integer 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 in the SharedPreferences.

Returns
int The value from the storage or the default return value

getPersistedLong

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 in the SharedPreferences.

Returns
long The value from the storage or the default return value

getPersistedString

protected String getPersistedString (String defaultReturnValue)

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

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 storage or the default return value

notifyChanged

protected void notifyChanged ()

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

notifyHierarchyChanged

protected void notifyHierarchyChanged ()

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

onAttachedToHierarchy

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

onClick

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.

onGetDefaultValue

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

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.

onRestoreInstanceState

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

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

protected void onSetInitialValue (Object defaultValue)

Implement this to set the initial value of the preference.

If you are persisting values to SharedPreferences or a PreferenceDataStore you should restore the saved value for the preference.

If you are not persisting values, or there is no value saved for the preference, you should set the value of the preference to defaultValue.

Parameters
defaultValue Object: The default value for the preference if set, otherwise null.

onSetInitialValue

protected void onSetInitialValue (boolean restorePersistedValue, 
                Object defaultValue)

This method is deprecated.
Use onSetInitialValue(Object) instead.

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

protected boolean persistBoolean (boolean value)

Attempts to persist a Boolean if this preference is persistent.

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
value boolean: The value to persist

Returns
boolean true if the preference is persistent, false otherwise

persistFloat

protected boolean persistFloat (float value)

Attempts to persist a Float if this preference is persistent.

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
value float: The value to persist

Returns
boolean true if the preference is persistent, false otherwise

persistInt

protected boolean persistInt (int value)

Attempts to persist an Integer if this preference is persistent.

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
value int: The value to persist

Returns
boolean true if the preference is persistent, false otherwise

persistLong

protected boolean persistLong (long value)

Attempts to persist a Long if this preference is persistent.

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
value long: The value to persist

Returns
boolean true if the preference is persistent, false otherwise

persistString

protected boolean persistString (String value)

Attempts to persist a String if this preference is persistent.

The returned value doesn't reflect whether the given value was persisted, since we may not necessarily commit if there will be a batch commit later.

Parameters
value String: The value to persist

Returns
boolean true if the preference is persistent, false otherwise

shouldPersist

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