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 fr