Preference

public class Preference
extends Object implements Comparable<Preference>

java.lang.Object
   ↳ androidx.preference.Preference


The basic building block that represents an individual setting displayed to a user in the preference hierarchy. This class provides the data that will be displayed to the user and has a reference to the SharedPreferences or PreferenceDataStore instance that persists the preference's values.

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 that represents the key that is used to persist the value to the device. It is up to the subclass to decide how to store the value.

Developer Guides

For information about building a settings screen using the AndroidX Preference library, see Settings.

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

Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference.

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)

This method is deprecated. Preferences aren't views. They should not need any accessibility changes, unless the view hierarchy is customized. In this situation, please add Accessibility information in onBindViewHolder(PreferenceViewHolder).

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 setCopyingEnabled(boolean enabled)

Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference.

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