Question

I'm using AndEngine to create a live wallpaper and SharedPreferences for the settings.

Here is my XML file which hosts the different settings a user can choose for the live wallpaper:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="Main Settings">
           <ListPreference
           android:title="Background Image"
           android:summary="Set the background image for the wallpaper"
           android:key="listPref"
           android:defaultValue="1"
           android:entries="@array/background"
           android:entryValues="@array/background_values" /> 
        </PreferenceCategory>
</PreferenceScreen>

My @array looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="background">
    <item>Background 1</item>
    <item>Background 2</item>
</string-array>

<string-array name="background_values">
    <item>1</item>
    <item>2</item>
</string-array>

</resources>

My main wallpaper activity uses this to check to see if a user has picked a new setting:

    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {            

        if (prefs.getString("listPref", "Background 1").equals(2))
                {
            Toast.makeText(this, "test", Toast.LENGTH_LONG).show();

    final Scene scene = new Scene();
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(
                    0, 0, 0, 5);
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(2.7f,
                    new Sprite(0, 0, this.mAutoParallaxCustomBackground)));
    scene.setBackground(autoParallaxBackground);
                }
    }

My question is... let's say I have 10 different backgrounds to choose from: Background 1, Background 2, Background 3... etc., will I have to keep making If Else statements like in the above method to be able to care for all the backgrounds in the settings? Or am I missing a step somewhere? There must be an easier way to figure out which background the user has clicked on in the settings.

Do I have to do something with the 'background_values' in my XML file?

And yes, I realize my If statement is incorrect but I haven't yet figured out how to get it to work properly.

Was it helpful?

Solution

Found the soulution:

Created this:

    @Override
    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {           
            settingsChanged = true;
    }

Added:

    @Override
    public void onResume(){
        super.onResume();
        if(settingsChanged)
        {
                BuildScene();
                settingsChanged = false;
        }
    }

(BuildScene()) is a call inside my onLoadScene() method.)

Then I just custom coded If, Else statements inside the BuildScene() method to figure out which option the user is currently using, and then applied the new image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top