Reputation: 4874
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>
Upvotes: 16
Views: 11945
Reputation: 1066
index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)
Upvotes: 2
Reputation: 12768
I found that sometime I need to clear application data. Uninstall and reinstall the app. After that, everything works as expected.
Upvotes: 34
Reputation: 424
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);
}
}
Upvotes: 11