Search code examples
androidandroid-preferenceslistpreference

Android setting a default for ListPreference in preferences XML, including setting the radiobutton


I have a listpreference in my pref_general.xml that I'm trying to set a default value for. Setting the "android:defaultValue="prefa" sets the summary, but when I click the preference, I still get two empty checkboxes with the two options. How do I set it so that one of them is selected by default? This is going to be a celcius / fahrenheit switch and I just want it to default to celcius.

<PreferenceCategory
    android:title="Category Title"
    android:key="options">

    <ListPreference
        android:key="prefName"
        android:entries="@array/prefaORprefb"
        android:summary="etc"
        android:defaultValue="prefa"
        android:entryValues="@array/prefaORprefb"
        android:title="Pick one" />

</PreferenceCategory>

I'm loading this through an activity layout with a lsitview. Cheers.


Solution

  • Make sure defaultValue is one of the actual values inside your entryValues array.

    Also try calling this at the start of your application: PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.pref_general, false);