Reputation: 27
I tried searching for an answer on here and on Google but didn't manage to get very far.
I'm trying to do a listPreference
on the Android platform allowing users to choose their text colour. Currently it's only returning the default value and I'm not quite sure why my if/else statement isn't working.
prefs.xml:
<ListPreference
android:title="textcolorpreference"
android:key="list1"
android:summary="Text Color"
android:entries="@array/list"
android:entryValues="@array/lValues"
/>
array.xml:
<string-array name="list">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
<item>Yellow</item>
</string-array>
<string-array name="lValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
Java:
SharedPreferences getPrefs =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String values = getPrefs.getString("list", "2");
if(values.contentEquals("1")){
tp.setColor(Color.RED);
} else if(values.contentEquals("2")){
tp.setColor(Color.GREEN);
} else if(values.contentEquals("3")){
tp.setColor(Color.BLUE);
}else if(values.contentEquals("4")){
tp.setColor(Color.YELLOW);
}
Currently only the default value of the list preference works e.g. if I put the listPreference default values a String values = getPrefs.getString("list", "2")
I get color.GREEN
which makes me think I need to add a preference changed listener, but I'm currently unable to implement it.
I'd be extremely grateful for any help you guys are able to provide !
Upvotes: 0
Views: 533
Reputation: 6711
You can retrieve the correct Integer from the prefecence list1
Integer values = getPrefs.getInt("list1", 2);
then you could switch from the values retrieved:
switch(values ) {
case 1:
tp.setColor(Color.RED);
break;
case 2:
tp.setColor(Color.GREEN);
break;
case 3:
tp.setColor(Color.BLUE);
break;
case 4:
tp.setColor(Color.YELLOW);
break;
default:
tp.setColor(Color.GREEN);
}
Upvotes: 1