Added in API level 14
Deprecated in API level 29

TwoStatePreference


abstract class TwoStatePreference : Preference
kotlin.Any
   ↳ android.preference.Preference
   ↳ android.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
Public constructors

TwoStatePreference(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

TwoStatePreference(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open Boolean

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

open CharSequence!

Returns the summary to be shown when unchecked.

open CharSequence!

Returns the summary to be shown when checked.

open Boolean

Returns the checked state.

open Unit
setChecked(checked: Boolean)

Sets the checked state and saves it to the SharedPreferences.

open Unit
setDisableDependentsState(disableDependentsState: Boolean)

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

open Unit
setSummaryOff(summaryResId: Int)

open Unit

Sets the summary to be shown when unchecked.

open Unit
setSummaryOn(summaryResId: Int)

open Unit

Sets the summary to be shown when checked.

open Boolean

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

Protected methods
open Unit

Processes a click on the preference.

open Any!

Called when a Preference is being inflated and the default value attribute needs to be read.

open Unit

Hook allowing a Preference to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState.

open 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.

open Unit
onSetInitialValue(restoreValue: Boolean, defaultValue: Any!)

Implement this to set the initial value of the Preference.

Inherited functions

Public constructors

TwoStatePreference

Added in API level 14
TwoStatePreference(context: Context!)

TwoStatePreference

Added in API level 14
TwoStatePreference(
    context: Context!,
    attrs: AttributeSet!)

TwoStatePreference

Added in API level 14
TwoStatePreference(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

TwoStatePreference

Added in API level 21
TwoStatePreference(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

getDisableDependentsState

Added in API level 14
open fun getDisableDependentsState(): Boolean

Deprecated: Deprecated in Java.

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

Return
Boolean Whether dependents are disabled when this preference is on (true) or when this preference is off (false).

getSummaryOff

Added in API level 14
open fun getSummaryOff(): CharSequence!

Deprecated: Deprecated in Java.

Returns the summary to be shown when unchecked.

Return
CharSequence! The summary.

getSummaryOn

Added in API level 14
open fun getSummaryOn(): CharSequence!

Deprecated: Deprecated in Java.

Returns the summary to be shown when checked.

Return
CharSequence! The summary.

isChecked

Added in API level 14
open fun isChecked(): Boolean

Deprecated: Deprecated in Java.

Returns the checked state.

Return
Boolean The checked state.

setChecked

Added in API level 14
open fun setChecked(checked: Boolean): Unit

Deprecated: Deprecated in Java.

Sets the checked state and saves it to the SharedPreferences.

Parameters
checked Boolean: The checked state.

setDisableDependentsState

Added in API level 14
open fun setDisableDependentsState(disableDependentsState: Boolean): Unit

Deprecated: Deprecated in Java.

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 API level 14
open fun setSummaryOff(summaryResId: Int): Unit

Deprecated: Deprecated in Java.

Parameters
summaryResId Int: The summary as a resource.

setSummaryOff

Added in API level 14
open fun setSummaryOff(summary: CharSequence!): Unit

Deprecated: Deprecated in Java.

Sets the summary to be shown when unchecked.

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

setSummaryOn

Added in API level 14
open fun setSummaryOn(summaryResId: Int): Unit

Deprecated: Deprecated in Java.

Parameters
summaryResId Int: The summary as a resource.

setSummaryOn

Added in API level 14
open fun setSummaryOn(summary: CharSequence!): Unit

Deprecated: Deprecated in Java.

Sets the summary to be shown when checked.

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

shouldDisableDependents

Added in API level 14
open fun shouldDisableDependents(): Boolean

Deprecated: Deprecated in Java.

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

Return
Boolean True if the dependents should be disabled, otherwise false.

Protected methods

onClick

Added in API level 14
protected open fun onClick(): Unit

Deprecated: Deprecated in Java.

Processes a click on the preference. This includes saving the value to the SharedPreferences. However, the overridden method should call callChangeListener(java.lang.Object) to make sure the client wants to update the preference's state with the new value.

onGetDefaultValue

Added in API level 14
protected open fun onGetDefaultValue(
    a: TypedArray!,
    index: Int
): Any!

Deprecated: Deprecated in Java.

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.
Return
Any! The default value of this preference type.

onRestoreInstanceState

Added in API level 14
protected open fun onRestoreInstanceState(state: Parcelable!): Unit

Deprecated: Deprecated in Java.

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 API level 14
protected open fun onSaveInstanceState(): Parcelable!

Deprecated: Deprecated in Java.

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.

Return
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 API level 14
protected open fun onSetInitialValue(
    restoreValue: Boolean,
    defaultValue: Any!
): Unit

Deprecated: Deprecated in Java.

Implement this to set the initial value of the Preference.

If restorePersistedValue is true, you should restore the Preference value from the android.content.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
restorePersistedValue True to restore the persisted value; false to use the given defaultValue.
defaultValue Any!: The default value for this Preference. Only use this if restorePersistedValue is false.