ListPreference


class ListPreference : DialogPreference

Known direct subclasses
DropDownPreference

A ListPreference that presents the options in a drop down menu rather than a dialog.


A Preference that displays a list of entries as a dialog.

This preference saves a string value. This string will be the value from the setEntryValues array.

name android:entries

name android:entryValues

Summary

Public constructors

ListPreference(context: Context, attrs: AttributeSet?)
ListPreference(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
ListPreference(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
)

Public functions

Int

Returns the index of the given value (in the entry values array).

Array<CharSequence!>!

The list of entries to be shown in the list in subsequent dialogs.

CharSequence?

Returns the entry corresponding to the current value.

Array<CharSequence!>!

Returns the array of values to be saved for the preference.

CharSequence?

Returns the summary of this preference.

String!

Returns the value of the key.

Unit

Sets the human-readable entries to be shown in the list.

Unit
setEntries(entriesResId: @ArrayRes Int)
Unit

The array to find the value to save for a preference when an entry from entries is selected.

Unit
setEntryValues(entryValuesResId: @ArrayRes Int)
Unit

Sets the summary for this preference with a CharSequence.

Unit
setValue(value: String!)

Sets the value of the key.

Unit

Sets the value to the given index from the entry values.

Protected functions

Any!

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

Unit

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

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.

Unit
onSetInitialValue(defaultValue: Any!)

Implement this to set the initial value of the preference.

Inherited Constants

From androidx.preference.Preference
const Int
DEFAULT_ORDER = 2147483647

Specify for setOrder if a specific order is not required.

Inherited functions

From java.lang.Comparable
abstract Int
compareTo(p: T!)
From androidx.preference.DialogPreference
Drawable?

Returns the icon to be shown on subsequent dialogs.

Int

Returns the layout resource that is used as the content view for subsequent dialogs.

CharSequence?

Returns the message to be shown on subsequent dialogs.

CharSequence?

Returns the title to be shown on subsequent dialogs.

CharSequence?

Returns the text of the negative button to be shown on subsequent dialogs.

CharSequence?

Returns the text of the positive button to be shown on subsequent dialogs.

Unit

Processes a click on the preference.

Unit
setDialogIcon(dialogIcon: Drawable?)

Sets the icon of the dialog.

Unit
setDialogIcon(dialogIconRes: Int)

Sets the icon (resource ID) of the dialog.

Unit
setDialogLayoutResource(dialogLayoutResId: Int)

Sets the layout resource that is inflated as the View to be shown as the content view of subsequent dialogs.

Unit
setDialogMessage(dialogMessage: CharSequence?)

Sets the message of the dialog.

Unit
setDialogMessage(dialogMessageResId: Int)
Unit

Sets the title of the dialog.

Unit
setDialogTitle(dialogTitleResId: Int)
Unit
setNegativeButtonText(negativeButtonText: CharSequence?)

Sets the text of the negative button of the dialog.

Unit
setNegativeButtonText(negativeButtonTextResId: Int)
Unit
setPositiveButtonText(positiveButtonText: CharSequence?)

Sets the text of the positive button of the dialog.

Unit
setPositiveButtonText(positiveButtonTextResId: Int)
From androidx.preference.Preference
Boolean
callChangeListener(newValue: Any!)

Call this method after the user changes the preference, but before the internal state is set.

Int

Compares preference objects based on order (if set), otherwise alphabetically on the titles.

T?

Finds a preference in the entire hierarchy (above or below this preference) with the given key.

Context

Returns the Context of this preference.

String?

Returns the key of the dependency on this preference.

Bundle

Return the extras Bundle object associated with this preference, creating a new Bundle if there currently isn't one.

String?

Return the fragment class name associated with this preference.

Drawable?

Returns the icon of this preference.

Intent?

Return the Intent associated with this preference.

String!

Gets the key for this preference, which is also the key used for storing values into SharedPreferences or PreferenceDataStore.

Int

Gets the layout resource that will be shown as the View for this preference.

Preference.OnPreferenceChangeListener?

Returns the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).

Preference.OnPreferenceClickListener?

Returns the callback to be invoked when this preference is clicked.

Int

Gets the order of this preference with respect to other preference objects on the same level.

PreferenceGroup?

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.

Boolean
getPersistedBoolean(defaultReturnValue: Boolean)

Attempts to get a persisted Boolean if this preference is persistent.

Float
getPersistedFloat(defaultReturnValue: Float)

Attempts to get a persisted Float if this preference is persistent.

Int
getPersistedInt(defaultReturnValue: Int)

Attempts to get a persisted Integer if this preference is persistent.

Long
getPersistedLong(defaultReturnValue: Long)

Attempts to get a persisted Long if this preference is persistent.

String!
getPersistedString(defaultReturnValue: String!)

Attempts to get a persisted set of Strings if this preference is persistent.

(Mutable)Set<String!>!
getPersistedStringSet(defaultReturnValue: (Mutable)Set<String!>!)

Attempts to get a persisted set of Strings if this preference is persistent.

PreferenceDataStore?

Returns PreferenceDataStore used by this preference.

PreferenceManager!

Gets the PreferenceManager that manages this preference object's tree.

SharedPreferences?

Returns the SharedPreferences where this preference can read its value(s).

Boolean

Checks whether this preference should disable its view when it's action is disabled.

Preference.SummaryProvider?

Returns the SummaryProvider used to configure the summary of this preference.

CharSequence?

Returns the title of this preference.

Int

Gets the layout resource for the controllable widget portion of this preference.

Boolean

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.

Boolean

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.

Boolean

Checks whether this preference should be visible to the user.

Unit

Should be called when the data of this Preference has changed.

Unit
notifyDependencyChange(disableDependents: Boolean)

Notifies any listening dependents of a change that affects the dependency.

Unit

Should be called when a preference has been added/removed from this group, or the ordering should be re-evaluated.

Unit

Called when the preference hierarchy has been attached to the list of preferences.

Unit

Called when this preference has been attached to a preference hierarchy.

Unit

Binds the created View to the data for this preference.

Unit
onDependencyChanged(dependency: Preference, disableDependent: Boolean)

Called when the dependency changes.

Unit

Called when the preference hierarchy has been detached from the list of preferences.

Unit

This function is deprecated.

Preferences aren't views.

Unit
onParentChanged(parent: Preference, disableChild: Boolean)

Called when the implicit parent dependency changes.

Unit

Called when this preference is being removed from the hierarchy.

Unit
onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any!)

This function is deprecated.

Use onSetInitialValue instead.

Bundle?

Return the extras Bundle object associated with this preference, returning null if there is not currently one.

Boolean

Attempts to persist a Boolean if this preference is persistent.

Boolean

Attempts to persist a Float if this preference is persistent.

Boolean
persistInt(value: Int)

Attempts to persist an Integer if this preference is persistent.

Boolean
persistLong(value: Long)

Attempts to persist a Long if this preference is persistent.

Boolean

Attempts to persist a String if this preference is persistent.

Boolean

Attempts to persist a set of Strings if this preference is persistent.

Unit

Restore this preference hierarchy's previously saved state from the given container.

Unit

Store this preference hierarchy's frozen state into the given container.

Unit

Sets whether the summary of this preference can be copied to the clipboard by long pressing on the preference.

Unit
setDefaultValue(defaultValue: Any!)

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.

Unit
setDependency(dependencyKey: String?)

Sets the key of a preference that this preference will depend on.

Unit
setEnabled(enabled: Boolean)

Sets whether this preference is enabled.

Unit
setFragment(fragment: String?)

Sets the class name of a fragment to be shown when this preference is clicked.

Unit
setIcon(icon: Drawable?)

Sets the icon for this preference with a Drawable.

Unit
setIcon(iconResId: Int)

Sets the icon for this preference with a resource ID.

Unit
setIconSpaceReserved(iconSpaceReserved: Boolean)

Sets whether to reserve the space of this preference icon view when no icon is provided.

Unit
setIntent(intent: Intent?)

Sets an Intent to be used for startActivity when this preference is clicked.

Unit
setKey(key: String!)

Sets the key for this preference, which is used as a key to the SharedPreferences or PreferenceDataStore.

Unit
setLayoutResource(layoutResId: Int)

Sets the layout resource that is inflated as the View to be shown for this preference.

Unit

Sets the callback to be invoked when this preference is changed by the user (but before the internal state has been updated).

Unit

Sets the callback to be invoked when this preference is clicked.

Unit
setOrder(order: Int)

Sets the order of this preference with respect to other preference objects on the same level.

Unit
setPersistent(persistent: Boolean)

Sets whether this preference is persistent.

Unit

Sets a PreferenceDataStore to be used by this preference instead of using SharedPreferences.

Unit
setSelectable(selectable: Boolean)

Sets whether this preference is selectable.

Unit
setShouldDisableView(shouldDisableView: Boolean)

Sets whether this preference should disable its view when it gets disabled.

Unit
setSingleLineTitle(singleLineTitle: Boolean)

Sets whether to constrain the title of this preference to a single line instead of letting it wrap onto multiple lines.

Unit
setSummary(summaryResId: Int)

Sets the summary for this preference with a resource ID.

Unit

Set a SummaryProvider that will be invoked whenever the summary of this preference is requested.

Unit

Sets the title for this preference with a CharSequence.

Unit
setTitle(titleResId: Int)

Sets the title for this preference with a resource ID.

Unit
setViewId(viewId: Int)

Set the ID that will be assigned to the overall View representing this preference, once bound.

Unit
setVisible(visible: Boolean)

Sets whether this preference should be visible to the user.

Unit
setWidgetLayoutResource(widgetLayoutResId: Int)

Sets the layout for the controllable widget portion of this preference.

Boolean

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

Boolean

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.

String

Public constructors

ListPreference

Added in 1.0.0
ListPreference(context: Context)

ListPreference

Added in 1.0.0
ListPreference(context: Context, attrs: AttributeSet?)

ListPreference

Added in 1.0.0
ListPreference(context: Context, attrs: AttributeSet?, defStyleAttr: Int)

ListPreference

Added in 1.0.0
ListPreference(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
)

Public functions

findIndexOfValue

Added in 1.0.0
fun findIndexOfValue(value: String!): Int

Returns the index of the given value (in the entry values array).

Parameters
value: String!

The value whose index should be returned

Returns
Int

The index of the value, or -1 if not found

getEntries

Added in 1.0.0
fun getEntries(): Array<CharSequence!>!

The list of entries to be shown in the list in subsequent dialogs.

Returns
Array<CharSequence!>!

The list as an array

getEntry

Added in 1.0.0
fun getEntry(): CharSequence?

Returns the entry corresponding to the current value.

Returns
CharSequence?

The entry corresponding to the current value, or null

getEntryValues

Added in 1.0.0
fun getEntryValues(): Array<CharSequence!>!

Returns the array of values to be saved for the preference.

Returns
Array<CharSequence!>!

The array of values

getSummary

fun getSummary(): CharSequence?

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
CharSequence?

The summary

getValue

Added in 1.0.0
fun getValue(): String!

Returns the value of the key. This should be one of the entries in getEntryValues.

Returns
String!

The value of the key

setEntries

Added in 1.0.0
fun setEntries(entries: Array<CharSequence!>!): Unit

Sets the human-readable entries to be shown in the list. This will be shown in subsequent dialogs.

Each entry must have a corresponding index in setEntryValues.

Parameters
entries: Array<CharSequence!>!

The entries

See also
setEntryValues

setEntries

Added in 1.0.0
fun setEntries(entriesResId: @ArrayRes Int): Unit
Parameters
entriesResId: @ArrayRes Int

The entries array as a resource

See also
setEntries

setEntryValues

Added in 1.0.0
fun setEntryValues(entryValues: Array<CharSequence!>!): Unit

The array to find the value to save for a preference when an entry from entries is selected. If a user clicks on the second item in entries, the second item in this array will be saved to the preference.

Parameters
entryValues: Array<CharSequence!>!

The array to be used as values to save for the preference

setEntryValues

Added in 1.0.0
fun setEntryValues(entryValuesResId: @ArrayRes Int): Unit
Parameters
entryValuesResId: @ArrayRes Int

The entry values array as a resource

See also
setEntryValues

setSummary

fun setSummary(summary: CharSequence?): Unit

Sets the summary for this preference with a CharSequence.

You can also use a SummaryProvider to dynamically configure the summary of this preference.

Parameters
summary: CharSequence?

The summary for the preference

Throws
java.lang.IllegalStateException

If a SummaryProvider has already been set.

setValue

Added in 1.0.0
fun setValue(value: String!): Unit

Sets the value of the key. This should be one of the entries in getEntryValues.

Parameters
value: String!

The value to set for the key

setValueIndex

Added in 1.0.0
fun setValueIndex(index: Int): Unit

Sets the value to the given index from the entry values.

Parameters
index: Int

The index of the value to set

Protected functions

onGetDefaultValue

protected fun onGetDefaultValue(a: TypedArray, index: Int): Any!

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
a: TypedArray

The set of attributes

index: Int

The index of the default value attribute

Returns
Any!

The default value of this preference type

onRestoreInstanceState

protected fun onRestoreInstanceState(state: Parcelable?): Unit

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

protected fun onSaveInstanceState(): 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. 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

protected fun onSetInitialValue(defaultValue: Any!): Unit

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: Any!

The default value for the preference if set, otherwise null.