3

In the settings section, there is a list preference (as shown below in the code) for colours but I can't seem to find the reason why the values don't show up. I have checked the internet but I can't seem to understand the difference between android:entries and android:entryValues. Could this be a problem in my code? Any help is appreciated. Thanks in advance.

Array Below

<string-array name="preferencebackground">
<item name="Red"/>
<item name="Green"/>
<item name="Blue"/>
<item name="Orange"/>
</string-array>
<string name="preferencebackground">Background Colour Preferences</string>
 <string-array name="preferencebackgroundvalues">
<item name="Red"/>
<item name="Green"/>
<item name="Blue"/>
<item name="Orange"/>
</string-array>

Pref_general.xml

    <ListPreference
    android:defaultValue="-1"
    android:entries="@array/preferencebackground"
    android:entryValues="@array/preferencebackgroundvalues"
    android:key="example_list1"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/preferencebackground" />
user2229066
  • 143
  • 3
  • 13

2 Answers2

4

The definitions of your arrays should look like:

<string-array name="entries">
    <item>Red</item>
    <item>Blue</item>
    <item>Green</item>
    <item>Black</item>
</string-array>

<string-array name="values">
    <item>#FF0000</item>
    <item>#0000FF</item>
    <item>#00FF00</item>
    <item>#000000</item>
</string-array>

In addition, I'd remove these lines from the preferences ...

android:negativeButtonText="@null"
android:positiveButtonText="@null"

... and, more important: set a default value that is part of the list.

<ListPreference
    android:defaultValue="Red"
    android:entries="@array/preferencebackground"
    android:entryValues="@array/preferencebackgroundvalues"
    android:key="example_list1"
    android:title="@string/preferencebackground" />

Assuming you stored the arrays in a file called arrays.xml and the preferences file is well-formed as well, it should work without any issues.

p.s. of course it's better to use a string reference instead of the hard-coded Red and color references instead of #FF0000 etc.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • Thanks but what is the difference between android:entries= and android:entryValues= and what would you put inside these arrays? – user2229066 Apr 02 '13 at 19:10
  • This is because the buttons on the list are showing but not the colour names. – user2229066 Apr 02 '13 at 19:11
  • `values` are the actual values of the preference whereas `entryValues` are the values that are displayed to the user. In your case it's the same as your arrays have the same items. – Trinimon Apr 02 '13 at 19:13
  • e.g entries in the array would be whereas entry values would be – user2229066 Apr 02 '13 at 19:14
  • You know what, I just checked my list preferences again and I made a mistake: your array lists are not defined in the right way. See edit above. – Trinimon Apr 02 '13 at 19:19
  • Thank you so much...its a lot of help for a beginner – user2229066 Apr 02 '13 at 19:24
  • How would you implement an if statement to the settings activity so the background changes colour? – user2229066 Apr 02 '13 at 19:26
  • Your preferences use the key `example_list1`, so you can get the color value using this key. For the rest check for instance http://stackoverflow.com/questions/4761686/how-to-set-background-color-of-activity-to-white-programmatically or http://stackoverflow.com/questions/8472349/how-to-set-bdbdbd-as-text-color-programmatically-to-a-text-view – Trinimon Apr 02 '13 at 19:35
-1

i ran into the same problem, and my mistake is on the arrays.xml,

this is my previous code..

 <string-array name="list">
        <item name="option 1">Option 1 </item>
        <item name="option 2">Option 2 </item>
        <item name="option 3">Option 3 </item>
        <item name="option 4">Option 4 </item>
    </string-array>

    <string-array name="lvalues">
        <item name="1">1</item>
        <item name="2">2</item>
        <item name="3">3</item>
        <item name="4">4</item>
    </string-array>

and this is my revised code.

 <string-array name="optionlist">
        <item name="option 1">Option 1 </item>
        <item name="option 2">Option 2 </item>
        <item name="option 3">Option 3 </item>
        <item name="option 4">Option 4 </item>
    </string-array>

    <string-array name="optionlistValues">
        <item name="1">1</item>
        <item name="2">2</item>
        <item name="3">3</item>
        <item name="4">4</item>
    </string-array>

hope this helps somehow.