ListPreference default value not showing up
Asked Answered
S

3

16

I tried to set the default value for a ListPreference but nothing shows up.

Can you check my code for any errors?

Thanks.

Truly, Emad

This is in the settings.xml file:

<PreferenceCategory android:title="Media:">
    <CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
        android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
        android:defaultValue="false" />

    <ListPreference android:title="Chime Volume"
        android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
        android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
        android:defaultValue="1" />

</PreferenceCategory>

This is in the arrays file:

<resources>

    <string-array name="chimeVolumeLabels">
    <item>Default</item>
    <item>Soft</item>
    <item>Medium</item>
    <item>Loud</item>
    </string-array>

    <string-array name="chimeVolumeValues">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    </string-array>
</resources>
Scandal answered 8/9, 2011 at 2:37 Comment(0)
A
34

I found that sometime I need to clear application data. Uninstall and reinstall the app. After that, everything works as expected.

Appoggiatura answered 23/11, 2012 at 10:13 Comment(4)
you saved hours of my life.Interbedded
This may be due to the fact that Android will set the default values only once- see thisBellicose
True, saved hours of debugging.Borghese
@Appoggiatura I know this is old, but how can I make the prefs change without fresh install? I want the users who have already downloaded the app from store, to get the changeFleam
B
11

I found that I have to call PreferenceManager.setDefaultValues() in my Preferences Activity in order for my default value to show up initially.

public class PreferencesActivity extends PreferenceActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This static call will reset default values only on the first ever read
        PreferenceManager.setDefaultValues(getBaseContext(), R.xml.settings, false);

        addPreferencesFromResource(R.xml.settings);
    }
}
Beanie answered 3/1, 2012 at 7:3 Comment(1)
+1 for this answer. Until preferences activity is invoked for the very first time after the application's install, the call to sharedPreferences.getString(key, null) will return null even if the android:defaultValue attribute is set in xml. If the default preferences values need to be available before the preferences activity is invoked in the application for the first time then PreferenceManager.setDefaultValues() call is a great solution.Manilla
I
2
index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)
Infusible answered 12/3, 2019 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.