Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListPreference default value not showing up

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>
like image 873
Emad-ud-deen Avatar asked Sep 08 '11 02:09

Emad-ud-deen


3 Answers

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

like image 103
Seraphim's Avatar answered Nov 11 '22 22:11

Seraphim's


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);
    }
}
like image 23
micah94 Avatar answered Nov 11 '22 22:11

micah94


index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)
like image 2
gor Avatar answered Nov 11 '22 23:11

gor