TwoStatePreference
public abstract class TwoStatePreference extends Preference
CheckBoxPreference |
A |
SwitchPreferenceCompat |
A |
SwitchPreference |
A |
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.
Summary
Protected fields |
|
|---|---|
boolean |
Public constructors |
|---|
TwoStatePreference(@NonNull Context context) |
TwoStatePreference(@NonNull Context context, @Nullable AttributeSet attrs) |
TwoStatePreference( |
TwoStatePreference( |
Public methods |
|
|---|---|
boolean |
Returns whether dependents are disabled when this preference is on ( |
@Nullable CharSequence |
Returns the summary to be shown when unchecked. |
@Nullable CharSequence |
Returns the summary to be shown when checked. |
boolean |
Returns the checked state. |
void |
setChecked(boolean checked)Sets the checked state and saves it. |
void |
setDisableDependentsState(boolean disableDependentsState)Sets whether dependents are disabled when this preference is on ( |
void |
setSummaryOff(@Nullable CharSequence summary)Sets the summary to be shown when unchecked. |
void |
setSummaryOff(int summaryResId) |
void |
setSummaryOn(@Nullable CharSequence summary)Sets the summary to be shown when checked. |
void |
setSummaryOn(int summaryResId) |
boolean |
Checks whether this preference's dependents should currently be disabled. |
Protected methods |
|
|---|---|
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 |
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(Object defaultValue)Implement this to set the initial value of the preference. |
void |
syncSummaryView(@NonNull PreferenceViewHolder holder)Sync a summary holder contained within holder's sub-hierarchy with the correct summary text. |
Inherited Constants |
||
|---|---|---|
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
TwoStatePreference
public TwoStatePreference(@NonNull Context context, @Nullable AttributeSet attrs)
TwoStatePreference
public TwoStatePreference(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr
)
TwoStatePreference
public TwoStatePreference(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes
)
Public methods
getDisableDependentsState
public boolean getDisableDependentsState()
Returns whether dependents are disabled when this preference is on (true) or when this preference is off (false).
| Returns | |
|---|---|
boolean |
Whether dependents are disabled when this preference is on ( |
getSummaryOff
public @Nullable CharSequence getSummaryOff()
Returns the summary to be shown when unchecked.
| Returns | |
|---|---|
@Nullable CharSequence |
The summary |
getSummaryOn
public @Nullable CharSequence getSummaryOn()
Returns the summary to be shown when checked.
| Returns | |
|---|---|
@Nullable CharSequence |
The summary |
isChecked
public boolean isChecked()
Returns the checked state.
| Returns | |
|---|---|
boolean |
The checked state |
setChecked
public void setChecked(boolean checked)
Sets the checked state and saves it.
| Parameters | |
|---|---|
boolean checked |
The checked state |
setDisableDependentsState
public void setDisableDependentsState(boolean disableDependentsState)
Sets whether dependents are disabled when this preference is on (true) or when this preference is off (false).
| Parameters | |
|---|---|
boolean disableDependentsState |
The preference state that should disable dependents |
setSummaryOff
public void setSummaryOff(@Nullable CharSequence summary)
Sets the summary to be shown when unchecked.
For more complex configuration of this preference's summary, you should use a Preference.SummaryProvider
| Parameters | |
|---|---|
@Nullable CharSequence summary |
The summary to be shown when unchecked |
setSummaryOff
public void setSummaryOff(int summaryResId)
| Parameters | |
|---|---|
int summaryResId |
The summary as a resource |
| See also | |
|---|---|
setSummaryOff |
setSummaryOn
public void setSummaryOn(@Nullable CharSequence summary)
Sets the summary to be shown when checked.
For more complex configuration of this preference's summary, you should use a Preference.SummaryProvider
| Parameters | |
|---|---|
@Nullable CharSequence summary |
The summary to be shown when checked |
setSummaryOn
public void setSummaryOn(int summaryResId)
| Parameters | |
|---|---|
int summaryResId |
The summary as a resource |
| See also | |
|---|---|
setSummaryOn |
shouldDisableDependents
public boolean shouldDisableDependents()
Checks whether this preference's dependents should currently be disabled.
| Returns | |
|---|---|
boolean |
|
Protected methods
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 |
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(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 | |
|---|---|
Object defaultValue |
The default value for the preference if set, otherwise |
syncSummaryView
protected void syncSummaryView(@NonNull PreferenceViewHolder holder)
Sync a summary holder contained within holder's sub-hierarchy with the correct summary text.
| Parameters | |
|---|---|
@NonNull PreferenceViewHolder holder |
A |