user21597761
user21597761

Reputation:

Android Preferences defaultValue not works

Here is my preferences.xml

<PreferenceCategory android:title="@string/pref_cat_recognition">
    <ListPreference
        android:entries="@array/unitsArray"
        android:entryValues="@array/unitsValues"
        android:icon="@null"
        android:key="unit"
        android:summary="@string/units_summary"
        android:title="@string/units"
        android:defaultValue="1"/>
</PreferenceCategory>

and my array.xml

<string-array name="unitsArray" translatable="false">
    <item>cm</item>
    <item>inch</item>
</string-array>

<string-array name="unitsValues" translatable="false">
    <item>1</item>
    <item>2</item>
</string-array>

So it should be cm checked on start but there is no value checked. Why and how to solve that to have a value cm on start?

Upvotes: 1

Views: 101

Answers (2)

user21597761
user21597761

Reputation:

It turned out in manifest I had android:allowBackup="true"

Upvotes: 0

Arunm619
Arunm619

Reputation: 11

Your code is working fine. The reason why you dont see the selected value on the summary is because you have specified a custom summary text. You can use useSimpleSummaryProvider to enable the simple version of summary, also enabling the selected value seen without opening the preference.

Pls check my code below for clarity of things:

`<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="Choose Gender">
    <ListPreference
        app:defaultValue="m"
        app:entries="@array/gender_entries"
        app:entryValues="@array/gender_values"
        app:key="gender"
        app:title="Your Gender"
        app:useSimpleSummaryProvider="true" />
</PreferenceCategory>


<PreferenceCategory app:title="StackOverflow">
    <ListPreference
        app:defaultValue="1"
        app:entries="@array/unitsArray"
        app:entryValues="@array/unitsValues"
        app:key="unit"
        app:title="Units"
        app:useSimpleSummaryProvider="true" />

    <ListPreference
        app:defaultValue="1"
        app:entries="@array/unitsArray"
        app:entryValues="@array/unitsValues"
        app:key="unit"
        app:summary="Your summary goes here"
        app:title="Units" />
</PreferenceCategory>

`

This is the preference screen view

If the summary was provided you'd be seeing the summary text and not the default/selected value like in the picture above.

Upvotes: 1

Related Questions