I am trying to make an options menu with an option to deactivate / activate notifications, I have a PreferenceScreen
defined in XML with a CheckBoxPreference
in
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:id="@+id/checkbox_Notifications"
android:key="notifications"
android:title="@string/preferences_notification_checkbox_title"
android:defaultValue="true"
android:summary="@string/preferences_notification_checkbox_summary"
android:persistent="true"
/>
</PreferenceScreen>
And then I have a SettingsActivity
that extends from PreferenceActivity
, but I do not know how to collect the CheckBoxPreference
of the resources, I get an error just like I do now
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
final CheckBoxPreference notifications = (CheckBoxPreference) findPreference("checkbox_Notifications");
notifications.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
boolean checked = Boolean.valueOf(newValue.toString());
SharedPreferences prefs = context.getSharedPreferences("prefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putBoolean("notifications", checked);
prefsEditor.commit();
return true;
}
});
}
}