How to set the Default Value of a ListPreference
Asked Answered
E

11

62

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.

Ellaelladine answered 4/3, 2011 at 17:34 Comment(0)
T
28

Have you tried:

setValueIndex(int index);
Toratorah answered 4/3, 2011 at 17:50 Comment(4)
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); }Deft
and what is int index? What if I wanted to set a defined string?Resnick
How do i retrieve the last value stored in persistent storage for a preference who was set for persistent storage?Byelaw
@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.Patrilocal
Z
115

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.

Zoubek answered 7/7, 2013 at 4:12 Comment(5)
What if opts_values is an integer-array? Or is that a bad idea?Shellans
Note that in XML, the first option starts at index 1 (i.e. android:defaultValue="1") instead of 0.Bove
@Keith if you use anything other than a string-array, you will get an error when it tries to find the corresponding item. see https://mcmap.net/q/323363/-sharedpreferences-listpreference-nullpointerexceptionFrightfully
My xml file has the app: prefix on everything for some reason... Everything works except the app:defaultValue attributeSusurrate
If your root element is androidx.preference.PreferenceScreen (or the support library equivalent) then you will need the app: prefixInexplicit
T
28

Have you tried:

setValueIndex(int index);
Toratorah answered 4/3, 2011 at 17:50 Comment(4)
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); }Deft
and what is int index? What if I wanted to set a defined string?Resnick
How do i retrieve the last value stored in persistent storage for a preference who was set for persistent storage?Byelaw
@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.Patrilocal
C
13

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
}
Commissure answered 6/2, 2014 at 22:20 Comment(0)
R
6

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" />
Rawalpindi answered 23/8, 2017 at 14:52 Comment(0)
B
3

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

Babbling answered 10/4, 2012 at 10:29 Comment(0)
F
3

Just for the record if someone else has this problem:

setValueIndex(int X) is setting the value @ index X to the default value - so it is probably what you are looking for.

Set this value AFTER you added the Values! (stupid mistake but took me half an hour)

Fluffy answered 25/7, 2013 at 14:2 Comment(0)
S
3
((ListPreference) findPreference("pref_language")).setValue(Locale
                .getDefault().getLanguage());

setValue() is ListPreference's method, and setDefaultvalue is Preference's method

Schechter answered 18/11, 2014 at 9:38 Comment(0)
A
3

Actually it's because the SharedPreferences will persist after you re-build the app. Uninstall it and try again.

Anaemic answered 9/9, 2016 at 5:24 Comment(0)
P
2

This is an old post, but here's another way to set the default value for ListPreference with the following line of code:

PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
Poultry answered 11/3, 2017 at 0:9 Comment(0)
S
2

Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.

Note: the <VALUE> is the actual value and not the index of string array.

If still its not working, try below steps.

  • Clear application data.
  • Uninstall and reinstall the app
  • Check the list preference, you will see the default value selected

Strange, I know but it worked in my case.

Sondrasone answered 7/11, 2018 at 4:51 Comment(0)
P
0

If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:

app:defaultValue="<Value_in_string-array_with_values>".

Additionally: defaultValue is not an index number, is the actual value from the values array.

I also recommend using a string resource for the default value and clearing the data, uninstalling the app, or removing the file:

  • <package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest>. Replace <package_name_in_app_manifest> with real name like com.example.yourapp.

I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true". Solved it by using the Android Studio File Explorer and deleting the file.

And here is my solution example:

res/xml/root_preferences.xml

<ListPreference
    
    app:key="mic"
    
    app:title="@string/select_mic_title"
 
    app:useSimpleSummaryProvider="true"
   
    app:entries="@array/mic_entries"

    app:entryValues="@array/mic_values"
    app:defaultValue="@string/default_mic"
    />

res/values/arrays

<!-- Mic Preference -->

<string-array name="mic_entries">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>


<string-array name="mic_values">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>

res/strings.xml

<string name="default_mic">Integrated</string>

Result: Setting Fragment with default list value of "Integrated" given by string resource @string/default_mic

Purdah answered 24/1, 2022 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.