0

I'm fairly new with Java development and I started building a simple Android app. My MainActivity creates a SurfaceView called 'GameView' where I've created bouncing balls. An options menu is also created (in my MainActivity) which has a 'Settings' item. When tapping on the settings button, a new PreferenceActivity starts called SettingsActivity. In the settings menu I have to simple checkboxes that have to control the background music and sound effects (mute/unmute).

My checkboxes in the settings.xml:

<CheckBoxPreference
     android:summary="Turn music on or off"
     android:defaultValue="true"
     android:title="Music"
     android:key="musicPref" 
/>
<CheckBoxPreference
     android:summary="Turn sounds on or off"
     android:defaultValue="true"
     android:title="Sounds"
     android:key="soundPref" 
/> 

I've used the following code in my public GameView function in order to get the value of the checkboxes:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
musicPlaying = sharedPrefs.getBoolean("musicPref", true );
soundPlaying = sharedPrefs.getBoolean("soundPref", true );

EDIT: Do I have to define a click event in my settings menu in order to pass the changed value of my checkbox to my gameview?

How do I pass the changed value to my gameview?

Tomjesch
  • 492
  • 4
  • 27

2 Answers2

0

You are using SharedPreferences to read the settings, so you need to write there the values, something like:

ToggleButton tBsound;
tBsound.setChecked(sharedPrefs.getBoolean("soundPref",true));
SharedPreferences.Editor editor=sharedPrefs.edit();
tBsound.setOnClickListener(new OnClickListener() {
   @Override public void onClick(View v) {
   //save settings
   if (tBsound.isChecked()) editor.putBoolean("soundPref",true);
      else editor.putBoolean("soundPref",false); 
   editor.commit();
   }
});

This sample uses a ToggleButton, just change it accordingly if you're using a checkbox. Isolated code to write the settings looks like:

if (Build.VERSION.SDK_INT>=11)
   mySettings = getSharedPreferences(PREFS, MODE_MULTI_PROCESS);
else mySettings = getSharedPreferences(PREFS,0);
SharedPreferences.Editor editor = mySettings.edit();
editor.putBoolean("data",true);
editor.commit();

You'll need the MODE_MULTI_PROCESS tag if there are multiple processes (like a service) accessing the SharedPreferences, to force android clean the cached values every time.

Joanmi Bardera
  • 351
  • 1
  • 7
  • TY Joanmi for your answer but can you elaborate a bit more? If I understand correctly, I listen for the button/checkbox changes in my SettingsActivity, but how do I get or know when a value is changed so that I know when to play a sound? And what about MODE_MULTI_PROCESS, what's that exactly? – Tomjesch Aug 21 '15 at 13:55
  • Hi Tomjesch! You have your answers here http://stackoverflow.com/questions/6496450/android-checkbox-preference and here http://developer.android.com/reference/android/content/Context.html#MODE_MULTI_PROCESS and more info on MODE_MULTI_PROCESS here http://stackoverflow.com/questions/27827678/use-sharedpreferences-on-multi-process-mode – Joanmi Bardera Aug 21 '15 at 23:49
0

Thanks Joanmi for getting me on the right track. I've did some extensive research and it turns out that changes made in a PreferenceActivity are saved automatically. There is no need for a listener, so using:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); musicPlaying = sharedPrefs.getBoolean("musicPref", true ); soundPlaying = sharedPrefs.getBoolean("soundPref", true );

in my GameView is enough in order to check for changes in my PreferenceActivity.

Tomjesch
  • 492
  • 4
  • 27