0

I am having a little trouble writing a correct if-else statement for using a checkbox preference to change the background bitmap of my live-wallpaper. Currently I have this method called up:

 public void setPreferences(SharedPreferences prefs) {
        //Introduce Preference Variables
        p = prefs;

        //Initialize Preference variables
        final boolean mySetting = PreferenceManager.getDefaultSharedPreferences(mContext)
.getBoolean(String.valueOf(R.string.touch), false);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(String.valueOf(mySetting), false);
            editor.commit();

        //If statement to set preference value
        if (prefs.getBoolean(String.valueOf(mySetting), true)) {
            GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, nebula, 0);
        }else{
            GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, stars, 0);
        }


    }

Where R.string.touch is the key to my checkbox preference in my XML document, like this:

<CheckBoxPreference
    android:key="@string/touch"
    android:title=""
    android:summary=""
    android:defaultValue="false"/>

So my question is how could I write a correct if-else statement or even a switch-case statement allowing me to switch the background bitmap? Thank you for any help given.

Dashboarrd
  • 169
  • 1
  • 2
  • 11
  • what problem getting with current implementation ? – ρяσѕρєя K Jul 01 '15 at 02:53
  • What is the behavior you are seeing with your current code. For example, does the if then statement always return one result regaurdless of what you have stored in the shared preferences? – wolfaviators Jul 01 '15 at 02:53
  • Shared preferences (the source of the value) has nothing to do with the if-else (how the fetched value is used). Take time to address the *actual* problem, making sure to clearly describe it, which is may be with the actual vs expected returned preference value. – user2864740 Jul 01 '15 at 03:08
  • @wolfaviators I am getting literally nothing, it always crashes when I start the app up. – Dashboarrd Jul 01 '15 at 03:15

2 Answers2

1

One issue I see is that String.valueOf(R.string.touch) is going to be giving you an integer as a string rather than the value of the string from your strings.xml.

Here's another way that you could do it:

public void setPreferences() {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    final String key = mContext.getString(R.string.touch_setting_key);
    final boolean defaultValue = false;

    // Read setting's current value.
    final boolean currentValue = preferences.getBoolean(key, defaultValue);

    // Update setting to true.
    preferences.edit()
        .putBoolean(key, true)
        .apply();

    // Use new setting value in an if-statement
    final boolean newValue = preferences.getBoolean(key, defaultValue);

    if (newValue == true) {
        GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, nebula, 0);
    } else {
        GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, stars, 0);
    }
}

Here I'm assuming you're storing your key name in the XML like as follows:

<string translatable="false" name="touch_setting_key">touch_setting_key</string>

As you use the settings in more places, you can store the preferences variable with the class instance as mPreferences and initialize it from onCreate().

You can also store default values in the XML. For a boolean you'd do it like this:

<item name="defaultTouchSetting" format="boolean" type="bool">true</item>

And to read that from code you'd do this:

final boolean defaultValue = mContext.getResources().getBoolean(R.bool.defaultTouchSetting);

Let me know if you're still stuck on anything.

Learn OpenGL ES
  • 4,759
  • 1
  • 36
  • 38
  • It half works. What I did was I switched the newValue with the currentValue in the if statement. Now when the app starts it starts off with the nebula scene, then when I uncheck the checkbox it switches to the star scene. But i noticed 2 problems. When I click the checkbox it stays as checked, and when I try to put the nebula scene back it stays as the stars scene, any thoughts? – Dashboarrd Jul 01 '15 at 17:32
  • Is the "Update setting to true" part still there? You wouldn't need that if you're using a Preferences activity. – Learn OpenGL ES Jul 02 '15 at 13:11
0

The problem could be in how you are accessing sharedPreferences. In the past, when I have used shared preferences, I used a slightly different aproach: getActivity().getSharedPreferences(). In researching the PreferencManager.getDefault preferences vs Context.getSharedPreferences, I found this post. Android getDefaultSharedPreferences I would try replacing the

PreferenceManager.getDefaultPreferences(mContext);

with

mContext.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);

Where "myAppPrefs" is an arbitrary key.

I hope this helps. Good luck!

Community
  • 1
  • 1
wolfaviators
  • 503
  • 1
  • 7
  • 21
  • It did help, I think. My app was crashing before but since I added what you suggested it isn't crashing. However when I click the checkbox nothing happens, it does not change the background bitmap,probably has something to do with the if statement. Thanks for the help! – Dashboarrd Jul 01 '15 at 03:25
  • One other thing I noticed that might be causing some problems is the line of code String.valueOf(R.string.touch). the R file just contains a bunch of integers for memory addresses. Consider replacing it with activity.getResources().getString(R.string.touch). that will give you the string that is stored in the resourch rather than a number. – wolfaviators Jul 01 '15 at 03:30
  • Also, when putting values in the shared prefferences, the first parameter is a key. I'm looking at a line of code "editor.putBoolean(String.valueOf(mySetting), false);" where mySetting is of type boolean. Consider changing it to prefs.putBoolean("SETTING_LABLE", true) where "SETTING_LABLE" describes the data stored there. – wolfaviators Jul 01 '15 at 03:35
  • Ok, I thought that mySetting would be the key because I stored the key value inside of it. – Dashboarrd Jul 01 '15 at 03:39
  • Putting the key as a string "mySetting" could be a fine way to specify that whatever true or false is stored there is the one connected to the setting in question. – wolfaviators Jul 01 '15 at 03:52
  • This is an aditional link for using shared preferences. http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values?rq=1 If you found my answer helpful, feel free to mark it as the accepted answer. Good luck! – wolfaviators Jul 01 '15 at 03:54
  • Thank you for the help wolf, I am about to go to bed as it is 12 where I live, the preference part is still not working, but I will keep on it tomorrow. – Dashboarrd Jul 01 '15 at 03:59