Added in API level 1
Deprecated in API level 29

ListPreference


open class ListPreference : DialogPreference
kotlin.Any
   ↳ android.preference.Preference
   ↳ android.preference.DialogPreference
   ↳ android.preference.ListPreference

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

This preference will store a string into the SharedPreferences. This string will be the value from the setEntryValues(java.lang.CharSequence[]) array.

Summary

XML attributes
android:entries The human-readable array to present as a list.
android:entryValues The array to find the value to save for a preference when an entry from entries is selected.
Inherited XML attributes
Inherited constants
Public constructors

ListPreference(context: Context!, attrs: AttributeSet!)

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

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

Public methods
open Int

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

open Array<CharSequence!>!

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

open CharSequence!

Returns the entry corresponding to the current value.

open Array<CharSequence!>!

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

open CharSequence!

Returns the summary of this ListPreference.

open String!

Returns the value of the key.

open Unit
setEntries(entriesResId: Int)

open Unit

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

open Unit
setEntryValues(entryValuesResId: Int)

open Unit

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

open Unit

Sets the summary for this Preference with a CharSequence.

open Unit
setValue(value: String!)

Sets the value of the key.

open Unit

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

Protected methods
open Unit
onDialogClosed(positiveResult: Boolean)

Called when the dialog is dismissed and should be used to save data to the SharedPreferences.

open Any!

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

open Unit

Prepares the dialog builder to be shown when the preference is clicked.

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

XML attributes

android:entries

android:entries
The human-readable array to present as a list. Each entry must have a corresponding index in entryValues.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:entryValues

android:entryValues
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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Public constructors

ListPreference

Added in API level 1
ListPreference(context: Context!)

ListPreference

Added in API level 1
ListPreference(
    context: Context!,
    attrs: AttributeSet!)

ListPreference

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

ListPreference

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

Public methods

findIndexOfValue

Added in API level 1
open fun findIndexOfValue(value: String!): Int

Deprecated: Deprecated in Java.

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

Parameters
value String!: The value whose index should be returned.
Return
Int The index of the value, or -1 if not found.

getEntries

Added in API level 1
open fun getEntries(): Array<CharSequence!>!

Deprecated: Deprecated in Java.

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

Return
Array<CharSequence!>! The list as an array.

getEntry

Added in API level 1
open fun getEntry(): CharSequence!

Deprecated: Deprecated in Java.

Returns the entry corresponding to the current value.

Return
CharSequence! The entry corresponding to the current value, or null.

getEntryValues

Added in API level 1
open fun getEntryValues(): Array<CharSequence!>!

Deprecated: Deprecated in Java.

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

Return
Array<CharSequence!>! The array of values.

getSummary

Added in API level 1
open fun getSummary(): CharSequence!

Deprecated: Deprecated in Java.

Returns the summary of this ListPreference. If the summary has a java.lang.String#format marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place.

Return
CharSequence! the summary with appropriate string substitution

getValue

Added in API level 1
open fun getValue(): String!

Deprecated: Deprecated in Java.

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

Return
String! The value of the key.

setEntries

Added in API level 1
open fun setEntries(entriesResId: Int): Unit

Deprecated: Deprecated in Java.

Parameters
entriesResId Int: The entries array as a resource.

setEntries

Added in API level 1
open fun setEntries(entries: Array<CharSequence!>!): Unit

Deprecated: Deprecated in Java.

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(java.lang.CharSequence[]).

Parameters
entries Array<CharSequence!>!: The entries.

setEntryValues

Added in API level 1
open fun setEntryValues(entryValuesResId: Int): Unit

Deprecated: Deprecated in Java.

Parameters
entryValuesResId Int: The entry values array as a resource.

setEntryValues

Added in API level 1
open fun setEntryValues(entryValues: Array<CharSequence!>!): Unit

Deprecated: Deprecated in Java.

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.

setSummary

Added in API level 1
open fun setSummary(summary: CharSequence!): Unit

Deprecated: Deprecated in Java.

Sets the summary for this Preference with a CharSequence. If the summary has a java.lang.String#format marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place when it's retrieved.

Parameters
summary CharSequence!: The summary for the preference.

setValue

Added in API level 1
open fun setValue(value: String!): Unit

Deprecated: Deprecated in Java.

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 API level 1
open fun setValueIndex(index: Int): Unit

Deprecated: Deprecated in Java.

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

Parameters
index Int: The index of the value to set.

Protected methods

onDialogClosed

Added in API level 1
protected open fun onDialogClosed(positiveResult: Boolean): Unit

Deprecated: Deprecated in Java.

Called when the dialog is dismissed and should be used to save data to the SharedPreferences.

Parameters
positiveResult Boolean: Whether the positive button was clicked (true), or the negative button was clicked or the dialog was canceled (false).

onGetDefaultValue

Added in API level 1
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.

onPrepareDialogBuilder

Added in API level 1
protected open fun onPrepareDialogBuilder(builder: AlertDialog.Builder!): Unit

Deprecated: Deprecated in Java.

Prepares the dialog builder to be shown when the preference is clicked. Use this to set custom properties on the dialog.

Do not AlertDialog.Builder.create() or AlertDialog.Builder.show().

onRestoreInstanceState

Added in API level 1
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 1
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 1
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.