Preference
public class Preference implements Comparable
DialogPreference |
A base class for |
PreferenceGroup |
A container for multiple |
SeekBarPreference |
Preference based on android.preference.SeekBarPreference but uses support preference as a base . |
TwoStatePreference |
Common base class for preferences that have two selectable states, save a boolean value, and may have dependent preferences that are enabled/disabled based on the current state. |
CheckBoxPreference |
A |
DropDownPreference |
A |
EditTextPreference |
A |
ListPreference |
A |
MultiSelectListPreference |
A |
PreferenceCategory |
A container that is used to group similar |
PreferenceScreen |
A top-level container that represents a settings screen. |
SwitchPreferenceCompat |
A |
SwitchPreference |
A |
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.
name android:icon
name android:key
name android:title
name android:summary
name android:order
name android:fragment
name android:layout
name android:widgetLayout
name android:enabled
name android:selectable
name android:dependency
name android:persistent
name android:defaultValue
name android:shouldDisableView
name android:singleLineTitle
name android:iconSpaceReserved
Summary
Nested types |
|---|
public class Preference.BaseSavedState extends AbsSavedStateA base class for managing the instance state of a |
public interface Preference.OnPreferenceChangeListenerInterface definition for a callback to be invoked when the value of this |
public interface Preference.OnPreferenceClickListenerInterface definition for a callback to be invoked when a |
public interface Preference.SummaryProvider<T extends Preference>Interface definition for a callback to be invoked when the summary of this |
Constants |
|
|---|---|
static final int |
DEFAULT_ORDER = 2147483647Specify for |
Public constructors |
|---|
Preference(@NonNull Context context)Constructor to create a preference. |
Preference(@NonNull Context context, @Nullable AttributeSet attrs)Constructor that is called when inflating a preference from XML. |
Preference(Perform inflation from XML and apply a class-specific base style. |
Preference(Perform inflation from XML and apply a class-specific base style. |
Public methods |
|
|---|---|
boolean |
callChangeListener(Object newValue)Call this method after the user changes the preference, but before the internal state is set. |
int |
compareTo(@NonNull Preference another)Compares preference objects based on order (if set), otherwise alphabetically on the titles. |
@NonNull Context |
Returns the |
@Nullable String |
Returns the key of the dependency on this preference. |
@NonNull Bundle |
Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one. |
@Nullable String |
Return the fragment class name associated with this preference. |
@Nullable Drawable |
getIcon()Returns the icon of this preference. |
@Nullable Intent |
Return the |
String |
getKey()Gets the key for this preference, which is also the key used for storing values into |
final int |
Gets the layout resource that will be shown as the |
@Nullable Preference.OnPreferenceChangeListener |
Returns the callback to be invoked when this preference is changed by the user (but before the internal state has been updated). |
@Nullable Preference.OnPreferenceClickListener |
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. |
@Nullable PreferenceGroup |
Returns the |
Set<String> |
getPersistedStringSet(Set<String> defaultReturnValue)Attempts to get a persisted set of Strings if this preference is persistent. |
@Nullable PreferenceDataStore |
Returns |
PreferenceManager |
Gets the |
@Nullable SharedPreferences |
Returns the |
boolean |
Checks whether this preference should disable its view when it's action is disabled. |
@Nullable CharSequence |
Returns the summary of this preference. |
final @Nullable Preference.SummaryProvider |
Returns the |
@Nullable CharSequence |
getTitle()Returns the title of this preference. |
final int |
Gets the layout resource for the controllable widget portion of this preference. |
boolean |
hasKey()Checks whether this preference has a valid key. |
boolean |
Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference. |
boolean |
Checks whether this preference should be enabled in the list. |
boolean |
Returns whether the space of this preference icon view is reserved. |
boolean |
Checks whether this preference is persistent. |
boolean |
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 |
Gets whether the title of this preference is constrained to a single line. |
final boolean |
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 |
Called when the preference hierarchy has been attached to the list of preferences. |
void |
onBindViewHolder(@NonNull PreferenceViewHolder holder)Binds the created View to the data for this preference. |
void |
onDependencyChanged(Called when the dependency changes. |
void |
Called when the preference hierarchy has been detached from the list of preferences. |
void |
This method is deprecated. Preferences aren't views. |
void |
onParentChanged(@NonNull Preference parent, boolean disableChild)Called when the implicit parent dependency changes. |
@Nullable Bundle |
Return the extras Bundle object associated with this preference, returning |
boolean |
persistStringSet(Set<String> values)Attempts to persist a set of Strings if this preference is persistent. |
void |
restoreHierarchyState(@NonNull Bundle container)Restore this preference hierarchy's previously saved state from the given container. |
void |
saveHierarchyState(@NonNull 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(@Nullable 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(@Nullable String fragment)Sets the class name of a fragment to be shown when this preference is clicked. |
void |
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 |
Sets an |
void |
Sets the key for this preference, which is used as a key to the |
void |
setLayoutResource(int layoutResId)Sets the layout resource that is inflated as the |
void |
setOnPreferenceChangeListener(Sets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated). |
void |
setOnPreferenceClickListener(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(@Nullable PreferenceDataStore dataStore)Sets a |
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(@Nullable 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(@Nullable Preference.SummaryProvider summaryProvider)Set a |
void |
setTitle(@Nullable 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 |
Checks whether this preference's dependents should currently be disabled. |
@NonNull String |
toString() |
Protected methods |
|
|---|---|
@Nullable T |
<T extends Preference> findPreferenceInHierarchy(@NonNull String key)Finds a preference in the entire hierarchy (above or below this preference) with the given key. |
boolean |
getPersistedBoolean(boolean defaultReturnValue)Attempts to get a persisted |
float |
getPersistedFloat(float defaultReturnValue)Attempts to get a persisted |
int |
getPersistedInt(int defaultReturnValue)Attempts to get a persisted |
long |
getPersistedLong(long defaultReturnValue)Attempts to get a persisted |
String |
getPersistedString(String defaultReturnValue)Attempts to get a persisted set of Strings if this preference is persistent. |
void |
Should be called when the data of this |
void |
Should be called when a preference has been added/removed from this group, or the ordering should be re-evaluated. |
void |
onAttachedToHierarchy(@NonNull PreferenceManager preferenceManager)Called when this preference has been attached to a preference hierarchy. |
void |
onClick()Processes a click on the preference. |
@Nullable Object |
onGetDefaultValue(@NonNull TypedArray a, int index)Called when a preference is being inflated and the default value attribute needs to be read. |
void |
Called when this preference is being removed from the hierarchy. |
void |
onRestoreInstanceState(@Nullable Parcelable state)Hook allowing a preference to re-apply a representation of its internal state that had previously been generated by |
@Nullable Parcelable |
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(@Nullable Object defaultValue)Implement this to set the initial value of the preference. |
void |
This method is deprecated. Use |
boolean |
persistBoolean(boolean value)Attempts to persist a |
boolean |
persistFloat(float value)Attempts to persist a |
boolean |
persistInt(int value)Attempts to persist an |
boolean |
persistLong(long value)Attempts to persist a |
boolean |
persistString(String value)Attempts to persist a |
boolean |
Checks whether, at the given time this method is called, this preference should store/restore its value(s) into the |
Constants
DEFAULT_ORDER
public static final int DEFAULT_ORDER = 2147483647
Specify for setOrder if a specific order is not required.
Public constructors
Preference
public Preference(@NonNull Context context)
Constructor to create a preference.
| Parameters | |
|---|---|
@NonNull Context context |
The Context this is associated with, through which it can access the current theme, resources, |
Preference
public Preference(@NonNull Context context, @Nullable 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 | |
|---|---|
@NonNull Context context |
The Context this is associated with, through which it can access the current theme, resources, |
@Nullable AttributeSet attrs |
The attributes of the XML tag that is inflating the preference |
| See also | |
|---|---|
Preference |
Preference
public Preference(
@NonNull Context context,
@Nullable 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 | |
|---|---|
@NonNull Context context |
The Context this is associated with, through which it can access the current theme, resources, |
@Nullable AttributeSet attrs |
The attributes of the XML tag that is inflating the preference |
int defStyleAttr |
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. |
| See also | |
|---|---|
Preference |
Preference
public Preference(
@NonNull Context context,
@Nullable 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 | |
|---|---|
@NonNull Context context |
The |
@Nullable AttributeSet attrs |
The attributes of the XML tag that is inflating the preference |
int defStyleAttr |
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. |
int defStyleRes |
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. |
| See also | |
|---|---|
Preference |
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 | |
|---|---|
Object newValue |
The new value of this preference |
| Returns | |
|---|---|
boolean |
|
compareTo
public int compareTo(@NonNull Preference another)
Compares preference objects based on order (if set), otherwise alphabetically on the titles.
| Parameters | |
|---|---|
@NonNull Preference another |
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 @NonNull 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.
getDependency
public @Nullable String getDependency()
Returns the key of the dependency on this preference.
| See also | |
|---|---|
setDependency |
getExtras
public @NonNull 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.
getFragment
public @Nullable String getFragment()
Return the fragment class name associated with this preference.
| Returns | |
|---|---|
@Nullable String |
The fragment class name last set via |
getIcon
public @Nullable Drawable getIcon()
Returns the icon of this preference.
| See also | |
|---|---|
setIcon |
getIntent
public @Nullable Intent getIntent()
Return the Intent associated with this preference.
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 @Nullable 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 | |
|---|---|
@Nullable Preference.OnPreferenceChangeListener |
The callback to be invoked |
getOnPreferenceClickListener
public @Nullable Preference.OnPreferenceClickListener getOnPreferenceClickListener()
Returns the callback to be invoked when this preference is clicked.
| Returns | |
|---|---|
@Nullable 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 | |
|---|---|
setOrder |
getParent
public @Nullable 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 | |
|---|---|
@Nullable PreferenceGroup |
The parent PreferenceGroup or |
getPersistedStringSet
public Set<String> getPersistedStringSet(Set<String> defaultReturnValue)
Attempts to get a persisted set of Strings if this preference is persistent.
| Parameters | |
|---|---|
Set<String> defaultReturnValue |
The default value to return if either this preference is not persistent or this preference is not present. |
| See also | |
|---|---|
persistStringSet |
getPreferenceDataStore
public @Nullable 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 or to its PreferenceManager via setPreferenceDataStore.
| Returns | |
|---|---|
@Nullable PreferenceDataStore |
The |
getPreferenceManager
public PreferenceManager getPreferenceManager()
Gets the PreferenceManager that manages this preference object's tree.
| Returns | |
|---|---|
PreferenceManager |
getSharedPreferences
public @Nullable 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, getPersistedFloat, getPersistedInt, getPersistedLong, getPersistedString.
| Returns | |
|---|---|
@Nullable SharedPreferences |
The |
| See also | |
|---|---|
setPreferenceDataStore |
getShouldDisableView
public boolean getShouldDisableView()
Checks whether this preference should disable its view when it's action is disabled.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
setShouldDisableView |
getSummary
public @Nullable CharSequence getSummary()
Returns the summary of this preference. If a SummaryProvider has been set for this preference, it will be used to provide the summary returned by this method.
| Returns | |
|---|---|
@Nullable CharSequence |
The summary |
| See also | |
|---|---|
setSummary |
|
setSummaryProvider |
getSummaryProvider
public final @Nullable Preference.SummaryProvider getSummaryProvider()
Returns the SummaryProvider used to configure the summary of this preference.
| Returns | |
|---|---|
@Nullable Preference.SummaryProvider |
The |
| See also | |
|---|---|
Preference.SummaryProvider |
getTitle
public @Nullable CharSequence getTitle()
Returns the title of this preference.
| Returns | |
|---|---|
@Nullable CharSequence |
The title |
| See also | |
|---|---|
setTitle |
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 |
|
isCopyingEnabled
public boolean isCopyingEnabled()
Returns whether the summary of this preference can be copied to the clipboard by long pressing on the preference.
| Returns | |
|---|---|
boolean |
|
isEnabled
public boolean isEnabled()
Checks whether this preference should be enabled in the list.
| Returns | |
|---|---|
boolean |
|
isIconSpaceReserved
public boolean isIconSpaceReserved()
Returns whether the space of this preference icon view is reserved.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
setIconSpaceReserved |
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 |
|
isSelectable
public boolean isSelectable()
Checks whether this preference should be selectable in the list.
| Returns | |
|---|---|
boolean |
|
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 |
|
isSingleLineTitle
public boolean isSingleLineTitle()
Gets whether the title of this preference is constrained to a single line.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
setSingleLineTitle |
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 |
|
| See also | |
|---|---|
setVisible |
|
isShown |
notifyDependencyChange
public void notifyDependencyChange(boolean disableDependents)
Notifies any listening dependents of a change that affects the dependency.
| Parameters | |
|---|---|
boolean disableDependents |
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(@NonNull 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 | |
|---|---|
@NonNull PreferenceViewHolder holder |
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(
@NonNull Preference dependency,
boolean disableDependent
)
Called when the dependency changes.
| Parameters | |
|---|---|
@NonNull Preference dependency |
The preference that this preference depends on |
boolean disableDependent |
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.
@CallSuper
public voidonInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info)
Initializes an android.view.accessibility.AccessibilityNodeInfo with information about the View for this preference.
onParentChanged
public void onParentChanged(@NonNull Preference parent, boolean disableChild)
Called when the implicit parent dependency changes.
| Parameters | |
|---|---|
@NonNull Preference parent |
The preference that this preference depends on |
boolean disableChild |
Set true to disable this preference |
peekExtras
public @Nullable Bundle peekExtras()
Return the extras Bundle object associated with this preference, returning null if there is not currently one.
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.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
getPersistedStringSet |
restoreHierarchyState
public void restoreHierarchyState(@NonNull Bundle container)
Restore this preference hierarchy's previously saved state from the given container.
| See also | |
|---|---|
saveHierarchyState |
|
onRestoreInstanceState |
saveHierarchyState
public void saveHierarchyState(@NonNull Bundle container)
Store this preference hierarchy's frozen state into the given container.
| See also | |
|---|---|
restoreHierarchyState |
|
onSaveInstanceState |
setCopyingEnabled
public void setCopyingEnabled(boolean enabled)
Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference.
| Parameters | |
|---|---|
boolean enabled |
Set true to enable copying the summary 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 | |
|---|---|
Object defaultValue |
The default value |
setDependency
public void setDependency(@Nullable 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.
setEnabled
public void setEnabled(boolean enabled)
Sets whether this preference is enabled. If disabled, it will not handle clicks.
| Parameters | |
|---|---|
boolean enabled |
Set true to enable it |
setFragment
public void setFragment(@Nullable String fragment)
Sets the class name of a fragment to be shown when this preference is clicked.
setIcon
public void setIcon(@Nullable Drawable icon)
Sets the icon for this preference with a Drawable. This icon will be placed into the ID icon within the View created by onBindViewHolder.
setIcon
public void setIcon(int iconResId)
Sets the icon for this preference with a resource ID.
| Parameters | |
|---|---|
int iconResId |
The icon as a resource ID |
| See also | |
|---|---|
setIcon |
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 | |
|---|---|
boolean iconSpaceReserved |
Set |
setIntent
public void setIntent(@Nullable Intent intent)
Sets an Intent to be used for startActivity when this preference is clicked.
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 | |
|---|---|
String key |
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 widget_frame to be the parent of the specific widget for this preference. It should similarly contain title and summary.
It is an error to change the layout after adding the preference to a PreferenceGroup.
| Parameters | |
|---|---|
int layoutResId |
The layout resource ID to be inflated and returned as a |
| See also | |
|---|---|
setWidgetLayoutResource |
setOnPreferenceChangeListener
public void setOnPreferenceChangeListener(
@Nullable 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 | |
|---|---|
@Nullable Preference.OnPreferenceChangeListener onPreferenceChangeListener |
The callback to be invoked |
setOnPreferenceClickListener
public void setOnPreferenceClickListener(
@Nullable Preference.OnPreferenceClickListener onPreferenceClickListener
)
Sets the callback to be invoked when this preference is clicked.
| Parameters | |
|---|---|
@Nullable Preference.OnPreferenceClickListener 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 setOrderingAsAdded can be used to order preference objects based on the order they appear in the XML.
| Parameters | |
|---|---|
int order |
The order for this preference. A lower value will be shown first. Use |
| See also | |
|---|---|
setOrderingAsAdded |
|
DEFAULT_ORDER |
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 | |
|---|---|
boolean persistent |
Set |
setPreferenceDataStore
public void setPreferenceDataStore(@Nullable 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 | |
|---|---|
@Nullable PreferenceDataStore dataStore |
The |
| See also | |
|---|---|
setPreferenceDataStore |
setSelectable
public void setSelectable(boolean selectable)
Sets whether this preference is selectable.
| Parameters | |
|---|---|
boolean selectable |
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 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 | |
|---|---|
boolean shouldDisableView |
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 | |
|---|---|
boolean singleLineTitle |
Set |
setSummary
public void setSummary(@Nullable CharSequence summary)
Sets the summary for this preference with a CharSequence.
You can also use a SummaryProvider to dynamically configure the summary of this preference.
| Parameters | |
|---|---|
@Nullable CharSequence summary |
The summary for the preference |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If a |
| See also | |
|---|---|
setSummaryProvider |
setSummary
public void setSummary(int summaryResId)
Sets the summary for this preference with a resource ID.
You can also use a SummaryProvider to dynamically configure the summary of this preference.
| Parameters | |
|---|---|
int summaryResId |
The summary as a resource |
| See also | |
|---|---|
setSummary |
|
setSummaryProvider |
setSummaryProvider
public final void setSummaryProvider(@Nullable Preference.SummaryProvider summaryProvider)
Set a SummaryProvider that will be invoked whenever the summary of this preference is requested. Set null to remove the existing SummaryProvider.
| Parameters | |
|---|---|
@Nullable Preference.SummaryProvider summaryProvider |
The |
| See also | |
|---|---|
Preference.SummaryProvider |
setTitle
public void setTitle(@Nullable CharSequence title)
Sets the title for this preference with a CharSequence. This title will be placed into the ID title within the View bound by onBindViewHolder.
| Parameters | |
|---|---|
@Nullable CharSequence title |
The title for this preference |
setTitle
public void setTitle(int titleResId)
Sets the title for this preference with a resource ID.
| Parameters | |
|---|---|
int titleResId |
The title as a resource ID |
| See also | |
|---|---|
setTitle |
setViewId
public void setViewId(int viewId)
Set the ID that will be assigned to the overall View representing this preference, once bound.
| See also | |
|---|---|
setId |
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 findPreference.
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 | |
|---|---|
boolean visible |
Set false if this preference should be hidden from the user |
| See also | |
|---|---|
isShown |
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 | |
|---|---|
int widgetLayoutResId |
The layout resource ID to be inflated into the main layout |
| See also | |
|---|---|
setLayoutResource |
shouldDisableDependents
public boolean shouldDisableDependents()
Checks whether this preference's dependents should currently be disabled.
| Returns | |
|---|---|
boolean |
|
Protected methods
findPreferenceInHierarchy
protected @Nullable T <T extends Preference> findPreferenceInHierarchy(@NonNull String key)
Finds a preference in the entire hierarchy (above or below this preference) with the given key. Returns null if no preference could be found with the given key.
This only works after this preference has been attached to a hierarchy.
| Returns | |
|---|---|
@Nullable T |
The preference with the key, or |
| See also | |
|---|---|
findPreference |
getPersistedBoolean
protected boolean getPersistedBoolean(boolean defaultReturnValue)
Attempts to get a persisted Boolean if this preference is persistent.
| Parameters | |
|---|---|
boolean defaultReturnValue |
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 |
| See also | |
|---|---|
getPersistedString |
|
persistBoolean |
getPersistedFloat
protected float getPersistedFloat(float defaultReturnValue)
Attempts to get a persisted Float if this preference is persistent.
| Parameters | |
|---|---|
float defaultReturnValue |
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 |
| See also | |
|---|---|
getPersistedString |
|
persistFloat |
getPersistedInt
protected int getPersistedInt(int defaultReturnValue)
Attempts to get a persisted Integer if this preference is persistent.
| Parameters | |
|---|---|
int defaultReturnValue |
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 |
| See also | |
|---|---|
getPersistedString |
|
persistInt |
getPersistedLong
protected long getPersistedLong(long defaultReturnValue)
Attempts to get a persisted Long if this preference is persistent.
| Parameters | |
|---|---|
long defaultReturnValue |
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 |
| See also | |
|---|---|
getPersistedString |
|
persistLong |
getPersistedString
protected String getPersistedString(String defaultReturnValue)
Attempts to get a persisted set of Strings if this preference is persistent.
| Parameters | |
|---|---|
String defaultReturnValue |
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 |
| See also | |
|---|---|
persistString |
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(@NonNull PreferenceManager preferenceManager)
Called when this preference has been attached to a preference hierarchy. Make sure to call the super implementation.
| Parameters | |
|---|---|
@NonNull 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 to make sure the client wants to update the preference's state with the new value.
onGetDefaultValue
protected @Nullable Object onGetDefaultValue(@NonNull 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 getString.
| Parameters | |
|---|---|
@NonNull TypedArray a |
The set of attributes |
int index |
The index of the default value attribute |
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(@Nullable 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 | |
|---|---|
@Nullable Parcelable state |
The saved state that had previously been returned by |
| See also | |
|---|---|
onSaveInstanceState |
|
restoreHierarchyState |
onSaveInstanceState
protected @Nullable 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 | |
|---|---|
@Nullable Parcelable |
A Parcelable object containing the current dynamic state of this preference, or |
| See also | |
|---|---|
onRestoreInstanceState |
|
saveHierarchyState |
onSetInitialValue
protected void onSetInitialValue(@Nullable 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.
protected voidonSetInitialValue(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 | |
|---|---|
boolean restorePersistedValue |
True to restore the persisted value; false to use the given defaultValue. |
Object defaultValue |
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 | |
|---|---|
boolean value |
The value to persist |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
persistString |
|
getPersistedBoolean |
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 | |
|---|---|
float value |
The value to persist |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
persistString |
|
getPersistedFloat |
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 | |
|---|---|
int value |
The value to persist |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
persistString |
|
getPersistedInt |
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 | |
|---|---|
long value |
The value to persist |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
persistString |
|
getPersistedLong |
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 | |
|---|---|
String value |
The value to persist |
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
getPersistedString |
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 |
|