ListPreference
public class ListPreference extends DialogPreference
| java.lang.Object | |||
| ↳ | androidx.preference.Preference | ||
| ↳ | androidx.preference.DialogPreference | ||
| ↳ | androidx.preference.ListPreference |
DropDownPreference |
A |
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
Nested types |
|---|
public final class ListPreference.SimpleSummaryProvider implements Preference.SummaryProviderA simple |
Public constructors |
|---|
ListPreference(@NonNull Context context) |
ListPreference(@NonNull Context context, @Nullable AttributeSet attrs) |
ListPreference( |
ListPreference( |
Public methods |
|
|---|---|
int |
findIndexOfValue(String value)Returns the index of the given value (in the entry values array). |
CharSequence[] |
The list of entries to be shown in the list in subsequent dialogs. |
@Nullable CharSequence |
getEntry()Returns the entry corresponding to the current value. |
CharSequence[] |
Returns the array of values to be saved for the preference. |
@Nullable CharSequence |
Returns the summary of this preference. |
String |
getValue()Returns the value of the key. |
void |
setEntries(CharSequence[] entries)Sets the human-readable entries to be shown in the list. |
void |
setEntries(@ArrayRes int entriesResId) |
void |
setEntryValues(CharSequence[] entryValues)The array to find the value to save for a preference when an entry from entries is selected. |
void |
setEntryValues(@ArrayRes int entryValuesResId) |
void |
setSummary(@Nullable CharSequence summary)Sets the summary for this preference with a CharSequence. |
void |
Sets the value of the key. |
void |
setValueIndex(int index)Sets the value to the given index from the entry values. |
Protected methods |
|
|---|---|
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. |
Inherited Constants |
||
|---|---|---|
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
ListPreference
public ListPreference(@NonNull Context context, @Nullable AttributeSet attrs)
ListPreference
public ListPreference(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr
)
ListPreference
public ListPreference(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes
)
Public methods
findIndexOfValue
public int findIndexOfValue(String value)
Returns the index of the given value (in the entry values array).
| Parameters | |
|---|---|
String value |
The value whose index should be returned |
| Returns | |
|---|---|
int |
The index of the value, or -1 if not found |
getEntries
public CharSequence[] getEntries()
The list of entries to be shown in the list in subsequent dialogs.
| Returns | |
|---|---|
CharSequence[] |
The list as an array |
getEntry
public @Nullable CharSequence getEntry()
Returns the entry corresponding to the current value.
| Returns | |
|---|---|
@Nullable CharSequence |
The entry corresponding to the current value, or |
getEntryValues
public CharSequence[] getEntryValues()
Returns the array of values to be saved for the preference.
| Returns | |
|---|---|
CharSequence[] |
The array of values |
getSummary
public @Nullable CharSequence getSummary()
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 | |
|---|---|
@Nullable CharSequence |
The summary |
| See also | |
|---|---|
setSummary |
|
setSummaryProvider |
getValue
public String getValue()
Returns the value of the key. This should be one of the entries in getEntryValues.
| Returns | |
|---|---|
String |
The value of the key |
setEntries
public void setEntries(CharSequence[] entries)
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 | |
|---|---|
CharSequence[] entries |
The entries |
| See also | |
|---|---|
setEntryValues |
setEntries
public void setEntries(@ArrayRes int entriesResId)
| Parameters | |
|---|---|
@ArrayRes int entriesResId |
The entries array as a resource |
| See also | |
|---|---|
setEntries |
setEntryValues
public void setEntryValues(CharSequence[] 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.
| Parameters | |
|---|---|
CharSequence[] entryValues |
The array to be used as values to save for the preference |
setEntryValues
public void setEntryValues(@ArrayRes int entryValuesResId)
| Parameters | |
|---|---|
@ArrayRes int entryValuesResId |
The entry values array as a resource |
| See also | |
|---|---|
setEntryValues |
setSummary
public void setSummary(@Nullable CharSequence summary)
Sets the summary for this preference with a CharSequence.
You can also use a SummaryProvider to dynamically configure the summary of this preference.
| Parameters | |
|---|---|
@Nullable CharSequence summary |
The summary for the preference |
| Throws | |
|---|---|
java.lang.IllegalStateException |
If a |
| See also | |
|---|---|
setSummaryProvider |
setValue
public void setValue(String value)
Sets the value of the key. This should be one of the entries in getEntryValues.
| Parameters | |
|---|---|
String value |
The value to set for the key |
setValueIndex
public void setValueIndex(int index)
Sets the value to the given index from the entry values.
| Parameters | |
|---|---|
int index |
The index of the value to set |
Protected methods
onGetDefaultValue
protected 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 |
| Returns | |
|---|---|
Object |
The default value of this preference type |
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 |