Reputation: 2959
I'm trying to use SwitchPreference
and trying to detect it's state using isEnabled()
method.
Here's the code (in SettingsActivity.java
):
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// your stuff here
if (key.equals(KEY_ENABLE_F)) {
SwitchPreference fPref = (SwitchPreference) findPreference(key);
if (fPref.isEnabled()) {
Toast.makeText(getBaseContext(), "Enabled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Disabled", Toast.LENGTH_SHORT).show();
}
}
}
};
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(spChanged);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(spChanged);
}
The problem is that the Toast
with text "Enabled" is showing up no matter if switch in 'ON' or 'OFF'.
What could be wrong here?
Upvotes: 0
Views: 297
Reputation: 2959
Whoops! I should have tried it before posting the question. Anyways, I solved the issue by changing:
fPref.isEnabled()
to this:
fPref.isChecked()
Upvotes: 1