Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 1.0.0
belongs to Maven artifact androidx.preference:preference:1.0.0-rc01

TwoStatePreference

public abstract class TwoStatePreference
extends Preference

java.lang.Object
   ↳ androidx.preference.Preference
     ↳ androidx.preference.TwoStatePreference


Common base class for preferences that have two selectable states, persist a boolean value in SharedPreferences, and may have dependent preferences that are enabled/disabled based on the current state.

Summary

Inherited XML attributes

Inherited constants

Fields

protected boolean mChecked

Public constructors

TwoStatePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
TwoStatePreference(Context context, AttributeSet attrs, int defStyleAttr)
TwoStatePreference(Context context, AttributeSet attrs)
TwoStatePreference(Context context)

Public methods

boolean getDisableDependentsState()

Returns whether dependents are disabled when this preference is on (true) or when this preference is off (false).

CharSequence getSummaryOff()

Returns the summary to be shown when unchecked.

CharSequence getSummaryOn()

Returns the summary to be shown when checked.

boolean isChecked()

Returns the checked state.

void setChecked(boolean checked)

Sets the checked state and saves it to the SharedPreferences.

void setDisableDependentsState(boolean disableDependentsState)

Sets whether dependents are disabled when this preference is on (true) or when this preference is off (false).

void setSummaryOff(CharSequence summary)

Sets the summary to be shown when unchecked.

void setSummaryOff(int summaryResId)
void setSummaryOn(int summaryResId)
void setSummaryOn(CharSequence summary)

Sets the summary to be shown when checked.

boolean shouldDisableDependents()

Checks whether this preference's dependents should currently be disabled.

Protected methods

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 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 syncSummaryView(PreferenceViewHolder holder)

Sync a summary holder contained within holder's subhierarchy with the correct summary text.

Inherited methods

Fields

mChecked

added in version 1.0.0
protected boolean mChecked

Public constructors

TwoStatePreference

added in version 1.0.0
public TwoStatePreference (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

TwoStatePreference

added in version 1.0.0
public TwoStatePreference (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

TwoStatePreference

added in version 1.0.0
public TwoStatePreference (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TwoStatePreference

added in version 1.0.0
public TwoStatePreference (Context context)

Parameters
context Context

Public methods

getDisableDependentsState

added in version 1.0.0
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 (true) or when this preference is off (false).

getSummaryOff

added in version 1.0.0
public CharSequence getSummaryOff ()

Returns the summary to be shown when unchecked.

Returns
CharSequence The summary.

getSummaryOn

added in version 1.0.0
public CharSequence getSummaryOn ()

Returns the summary to be shown when checked.

Returns
CharSequence The summary.

isChecked

added in version 1.0.0
public boolean isChecked ()

Returns the checked state.

Returns
boolean The checked state.

setChecked

added in version 1.0.0
public void setChecked (boolean checked)

Sets the checked state and saves it to the SharedPreferences.

Parameters
checked boolean: The checked state.

setDisableDependentsState

added in version 1.0.0
public void setDisableDependentsState (boolean disableDependentsState)

Sets whether dependents are disabled when this preference is on (true) or when this preference is off (false).

Parameters
disableDependentsState boolean: The preference state that should disable dependents.

setSummaryOff

added in version 1.0.0
public void setSummaryOff (CharSequence summary)

Sets the summary to be shown when unchecked.

Parameters
summary CharSequence: The summary to be shown when unchecked.

setSummaryOff

added in version 1.0.0
public void setSummaryOff (int summaryResId)

Parameters
summaryResId int: The summary as a resource.

setSummaryOn

added in version 1.0.0
public void setSummaryOn (int summaryResId)

Parameters
summaryResId int: The summary as a resource.

setSummaryOn

added in version 1.0.0
public void setSummaryOn (CharSequence summary)

Sets the summary to be shown when checked.

Parameters
summary CharSequence: The summary to be shown when checked.

shouldDisableDependents

added in version 1.0.0
public boolean shouldDisableDependents ()

Checks whether this preference's dependents should currently be disabled.

Returns
boolean True if the dependents should be disabled, otherwise false.

Protected methods

onClick

added in version 1.0.0
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

added in version 1.0.0
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.

onRestoreInstanceState

added in version 1.0.0
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

added in version 1.0.0
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

added in version 1.0.0
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.

syncSummaryView

added in version 1.0.0
protected void syncSummaryView (PreferenceViewHolder holder)

Sync a summary holder contained within holder's subhierarchy with the correct summary text.

Parameters
holder PreferenceViewHolder: PreferenceViewHolder which holds a reference to the summary view