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>
I found that sometime I need to clear application data. Uninstall and reinstall the app. After that, everything works as expected.
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);
}
}
index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With