Question

I have been making a live wallpaper and have finally succeeded so far, but now I would like to let the users choose the background they would like to have that I have in the drawable folder. I have been trying a few things but so far no luck passing this through. I have an xml file to read one of two images they can choose from (I figure if I can get one working they all should be the same) Here's how it reads so far "DarkBack" "MediumBackb" "LightBack"

<string-array name="frontleft_value">
    <item>"1"</item>
    <item>"2"</item>
    <item>"3"</item>
</string-array>bubble

So they choose from either one of three backgrounds

In the activity I have this:

mPrefs = UnderwaterActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);  
mPrefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPrefs, null);
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
   myOtherClass.myfrontleftimage = (Integer.parseInt(prefs.getString("front_sub_left_choice", "1")));

}

So this should get me a integer of 1 or 2 depending on which one is click in the settings of the livewallpaper. The background image is held in a different class but first I have to compare what they clicked, so I tried an if/else statement to no luck at all.

I tried strings but that didn't work so I changed it to int and had some luck but not all.

The if else went like this.

private int chooseImage(){
   int theImage = 0;
  if(myfrontleftimage == 1){
     theImage = R.drawable.image1;
   }else if (myfrontleftimage == 2){
     theImage = R.drawable.image2;
     }else{
     theImage = R.drawable.image3;
   }
    return theImage;
 }

Then I put this method into the background image so it can read it, I have a setting java file and implement the engine for shared preferences also but I have a feeling it's in my if else statement, what I want is to get the value of the preference and compare them if they equal to 1, 2, 3 if either one equals one of them then it loads that background image, makes sense in theory but not in practice obviously, any help would be greatly appreciated, if I can figure this one out then I can use it for sprites also that I have in the livewallpaper. Thanks in advance

EDIT: I found the issue so far, I put in the pref file this:

<string-array name="livewallpaper_back_names">
    <item>Brown</item>
    <item>Grey</item>
</string-array>

<string-array name="livewallpaper_back_value">
    <item>0x7f020000</item>
    <item>0x7f020001</item>
</string-array>

Then in the sharedpreferences file I try to parse the 0x7f020000 (which I want to use to pick the image with) into an int like so

public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key)
    {
              sackNum = Integer.parseInt(prefs.getString("livewallpaper_back", "0x7f020000"));
             }

But then I get this error that it can't be done

E/AndroidRuntime(340): java.lang.NumberFormatException: unable to parse '0x7f020000' as integer

So this is where I am stuck at the moment. I checked the log and the array does get passed through and changes no problem so this is where the issue lies and if anyone can help me parse this thing into an int I would greatly appreciate it. Thanks again for any help in advance.

Was it helpful?

Solution 2

Ok I figured this out with the help of the test pattern example found here: http://www.codeproject.com/KB/android/AndroidLiveWallpaper.aspx

First I add a third depth to the array.xml like

<string-array name="livewallpaper_back_names">
<item>Brown</item>
<item>Grey</item>
</string-array>

<string-array name="livewallpaper_back_value">
<item>brown</item>
<item>grey</item>
</string-array>

<integer-array name="brownback">
<item>0x7f020000</item>
</integer-array>

<integer-array name="greyback">
<item>0x7f020001</item>
</integer-array>

So now it will read the brown or grey then I add the getResources().getIdentifier to get which array the user picks in the settings menu of the livewallpaper like so:

public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
    {

        String blah;
        sackNum = prefs.getString("livewallpaper_back", "brown");

        int pid = getResources().getIdentifier(sackNum + "back", "array", getPackageName());
        int backImageArray[] = getResources().getIntArray(pid);
        int back = backImageArray[0];
        int theBackImage = back;
        blah = getString(back);
        Log.d(TAG, blah);

    }

the int pid gathers the array of either brown or grey by adding back (in the xml) and gets the array which I then put into the backImageArray choosing only the first one because thats the only one, next I change the array into an integer (int back = backImageArray[0];) so it can be read to get the image the user chooses which would be theBackImage or you caould just drop it right into the Bitmap as such

private Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(),theBackImage);

and presto it gets the image. If anyone has a better way of doing this please add to this but so far this works, Mind you you have to update in the code so it reads it and changes it right away but this is the answer I figured out to get the user to at least choose the image. Hope it helps someone else out. Sam

OTHER TIPS

You are listening for Shared preference changes, but you do not appear to be setting them.

Set the charedPreferences using and Editor when the user makes a selection.

Then in your activity, read the shared preference in the onCreate() method. That way it will check the savedPreference every time the activity starts.

The code for editing preferences is something like this:

public void onClick(DialogInterface dialog, int whichButton) {
                    SharedPreferences settings = getSharedPreferences("YOURPREFERENCENAME", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("name", "MyNameIsBob");
                    editor.putString("password", "MyPasswordIsEasy");
                    editor.commit();
                }

In your onCreate you would do something like this:

// Get any existing username and password saved
        // Restore preferences
           SharedPreferences settings = getSharedPreferences("YOURPREFERENCENAME", 0);
           user_name = settings.getString("name", "defaultNameValue");
           password = settings.getString("password", "defaultPasswordValue");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top