clayton33
clayton33

Reputation: 4216

how do i reference a checkbox in my options menu from my main activity in android?

i have an option to "mute" in my menu options like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
 xmlns:android="http://schemas.android.com/apk/res/android">

<CheckBoxPreference android:summary="Mute all sound effects" android:title="Mute"android:key="muteSound"></CheckBoxPreference>
</PreferenceScreen>

now how can i check if that checkbox is checked or not?

Upvotes: 0

Views: 482

Answers (2)

maid450
maid450

Reputation: 7486

You have to get the SharedPreferences:

//in the main activity you should set the default values in case user has never entered the preferences screen
PreferenceManager.setDefaultValues(this, R.xml.preferences_file, false);
preferences = PreferenceManager.getDefaultSharedPreferences(this);

and then read the value:

preferences.getBoolean("muteSound", true) //the second argument is the default value

Upvotes: 1

dave.c
dave.c

Reputation: 10908

Use the following in an Activity:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean muteSound = prefs.getBoolean("muteSound", false);

Upvotes: 1

Related Questions