Gnufabio Gnufabio - 11 months ago 91
Android Question

How to set the Default Value of a ListPreference

i need to set the defult value for a ListPreference when the Activity starts.
I've tried with

but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with

For example, suppose I have this array of values in the arrays.xml:

<string-array name="opts">

<string-array name="opts_values">

In the PreferenceScreen xml:

android:title="Colour select"
android:summary="Select your favourite"
android:entryValues="@array/opts_values" />

In the Activity I'd like to do something like this:

String mycolour;
if (something) {
} else {
ListPreference colour = (ListPreference) findPreference ("colour");

But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.

Answer Source

Have you tried:

setValueIndex(int index);