I am trying to implement android jetpack preference for my settings screen. everything is working good, when I click on MultiSelectListPreference, it shows the list of entries, but I have few questions,
here is a part of my pref.xml file
...
app:entries="@array/res_entries"
app:entryValues="@array/res_id_values"
app:defaultValue="@array/res_def_values" //this line is not working
...
If I set res_id_values
in arrays.xml file to be a integer-array
, then app is crashing.
My settingsFragment class extends PreferenceFragmentCompat and override onCreatePreferences
and in it, I have written
setPreferencesFromResource(R.xml.pref.xml, rootkey)
EDIT
My res_entries
array :
<string-array name="res_entries">
<item>apple</item>
<item>Mango</item>
<item>Guava</item>
</string-array>
My res_id_values
array :
<string-array name="res_id_values">
<item>1</item>
<item>2</item>
<item>12</item>
</string-array>
My res_def_values
array :
<string-array name="res_def_values">
<item>true</item>
<item>false</item>
<item>true</item>
</string-array>
So finally, I solved it. All thanks to @CommonsWare .
answer to my first question is that, we cannot use integer-array. Use a string array and store integer values in it. later when you get it, use Integer.parseInt().
now coming to second question, to store default value(let's say you want second and third item to be checked by default), use res_id_values in res_def_values. Don't use true/false or 0/1 like me.
eg: if in above question, If I want apple and guava to be checked by default, then my res_def_values will look like this:
<string-array name="res_def_values">
<item>1</item>
<item>12</item>
</string-array>