Question

I'm trying to change the background color of an activity using a listPreference. I have written some code but it's going straight to Yellow.

Here is the String Arrays:

<string-array name="backgroundColors">
    <item name="1">Red</item>
    <item name="2">Green</item>
    <item name="3">Blue</item>
    <item name="4">Yellow</item>
</string-array>

<string-array name="backgroundColorEntries">
    <item name="1">#FF0000</item>
    <item name="2">#00FF00</item>
    <item name="3">#0000FF</item>
    <item name="4">#FFFF00</item>
</string-array>

Here is where I made the list in the XML file:

<ListPreference
    android:key="prefSetBackground"
    android:entries="@array/backgroundColors"
    android:summary="Set the background color of the main page."
    android:entryValues="@array/backgroundColorEntries"
    android:title="@string/pref_1" />

And finally, here is the Java part:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    SharedPreferences getPrefs = PreferenceManager
        .getDefaultSharedPreferences(getBaseContext());
        String background_chooser = getPrefs
                .getString("prefSetBackground", "0");
        View view = findViewById(R.id.main_layout);

        if (background_chooser.equals("1")) {
            view.setBackgroundColor(Color.RED);
        } else if (background_chooser.equals("2")) {
            view.setBackgroundColor(Color.GREEN);
        } else if (background_chooser.equals("3")) {
            view.setBackgroundColor(Color.BLUE);
        } else {
            view.setBackgroundColor(Color.YELLOW);
        }

    super.onWindowFocusChanged(hasFocus);
}

I think I am doing something wrong here but I'm not sure what. I'm new to using ListPreferences so please bear with me.

Thanks in advance.

EDIT: From what I can tell on my end with the app running on my phone, it's defaulting to the last part meaning that the Red, Blue, and Green are returning false. The background color is turning yellow. When I change the preference, nothing happens.

EDIT #2: It seems that the listpreference and the Java are not linking together. I don't know what I'm doing wrong.

Was it helpful?

Solution

Try this

String background_chooser = getPrefs
            .getString("prefSetBackground", "1");

Instead of

String background_chooser = getPrefs
            .getString("prefSetBackground", "0");

Edit : Also set android:defaultValue="1" in pref xml file.

so now your list pref xml should be like this

<ListPreference
  android:key="prefSetBackground"
  android:entries="@array/backgroundColors"
  android:summary="Set the background color of the main page."
  android:entryValues="@array/backgroundColorEntries"
  android:title="@string/pref_1"
  android:defaultValue="1" />

You also need to replace this

<string-array name="backgroundColorEntries">
  <item name="1">#FF0000</item>
  <item name="2">#00FF00</item>
  <item name="3">#0000FF</item>
  <item name="4">#FFFF00</item>
</string-array>

By this

 <string-array name="backgroundColorEntries">
  <item name="1">1</item>
  <item name="2">2</item>
  <item name="3">3</item>
  <item name="4">4</item>
</string-array>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top