How to set the Default Value of a ListPreference

53,237

Solution 1

Have you tried:

setValueIndex(int index);

Solution 2

You don't need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example

<string-array name="opts">
    <item>red</item>
    <item>green</item>
    <item>blue</item>
</string-array>

<string-array name="opts_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

...

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values"
    android:defaultValue="2" />

here I selected 2 as a default value. Remember defaultvalue will be opts_values element.

Solution 3

Sorry my bad English.

  1. List item
  2. Retrieve the list Check if the value is null. If it is null set to the default value.

Code:

ListPreference dataPref = (ListPreference) findPreference("keyList");

if(dataPref.getValue() == null){
    dataPref.setValueIndex(0); //set to index of your deafult value
}

Solution 4

You can set your default value by using the key like this

<string-array name="syncFrequency">
    <item name="1">Block All Calls</item>
    <item name="2">Block Black List</item>
    <item name="3">Block Unknown Calls</item>
    <item name="4">Allow White List</item>
    <item name="5">Receive All Calls</item>
</string-array>




<string-array name="syncFrequencyValues">
    <item name="1">Block_All_Calls</item>
    <item name="2">Block_Black_List</item>
    <item name="3">Block_Unknown_Calls</item>
    <item name="4">Allow_White_List</item>
    <item name="5">Receive_All_Calls</item>
</string-array>



     <ListPreference
        android:key="prefSyncFrequency"
        android:entries="@array/syncFrequency"
        android:summary="%s"
        android:defaultValue="Block_Black_List"
        android:entryValues="@array/syncFrequencyValues"
        android:title="@string/call_block_options" />

Solution 5

or you can also try colour.setValue(mycolour);

Share:
53,237
Gnufabio
Author by

Gnufabio

Android Developer. Currently studing C#.

Updated on July 08, 2022

Comments

  • Gnufabio
    Gnufabio almost 2 years

    i need to set the defult value for a ListPreference when the Activity starts. I've tried with ListPreference.setDefaultvalue("value"); 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 android:defaultValue)

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

    <string-array name="opts">
        <item>red</item>
        <item>green</item>
        <item>blue</item>
    </string-array>
    
    <string-array name="opts_values">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>
    

    In the PreferenceScreen xml:

    <ListPreference
        android:title="Colour select"
        android:summary="Select your favourite"
        android:key="colour"
        android:entries="@array/opts"
        android:entryValues="@array/opts_values" />
    

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

    String mycolour;
    if (something) {
        mycolour="1";
    } else {
        mycolour="2";
    }
    ListPreference colour = (ListPreference) findPreference ("colour");
    colour.setDefaultValue(mycolour);
    

    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.

  • mtotschnig
    mtotschnig about 12 years
    I am not sure this is the correct solution, since setValue or setValueIndex will effectively set the value, even if the user already has provided a custom value that is stored in the preferences. What works for me is if (colour.getValue() == null) { colour.setValue(mycolour); }
  • ki9
    ki9 over 8 years
    What if opts_values is an integer-array? Or is that a bad idea?
  • Ali Bdeir
    Ali Bdeir over 7 years
    and what is int index? What if I wanted to set a defined string?
  • Kenny Worden
    Kenny Worden almost 7 years
    Note that in XML, the first option starts at index 1 (i.e. android:defaultValue="1") instead of 0.
  • lucidbrot
    lucidbrot over 6 years
    @Keith if you use anything other than a string-array, you will get an error when it tries to find the corresponding item. see stackoverflow.com/a/25355756/2550406
  • Akay
    Akay over 6 years
    How do i retrieve the last value stored in persistent storage for a preference who was set for persistent storage?
  • Elijah Mock
    Elijah Mock over 4 years
    My xml file has the app: prefix on everything for some reason... Everything works except the app:defaultValue attribute
  • Matt Robertson
    Matt Robertson about 4 years
    If your root element is androidx.preference.PreferenceScreen (or the support library equivalent) then you will need the app: prefix
  • Marcell
    Marcell almost 2 years
    @AliBdeir the index is the index inside the value-list that should be used. But like having been noted it is not the solution itself. This should not be the accepted answer.