If I have a onSharedPreferenceChanged event in my PreferenceActivity, that is checking if a CheckBoxPreference is checked or not and setting setEnabled on some other Preference, does the actual value of the other Preference get changed in the SharedPreferences, or do I have to manually set them?
What I have is:
public void onSharedPreferenceChanged(Settings sharedPreferences, String key)
{
CheckBoxPreference cbUpdatesEnabled = (CheckBoxPreference)getPreferenceScreen().findPreference("updatesenabled");
CheckBoxPreference cbVibrate = (CheckBoxPreference)getPreferenceScreen().findPreference("vibrate");
cbVibrate.setEnabled(cbUpdatesEnabled.isChecked());
}
Which toggles whether the "vibrate" checkbox is enabled or disabled in the UI, but then do I also have to use:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("vibrate", cbUpdatesEnabled.isChecked());
editor.commit();
Or is there some other way of updating the SharedPreferences? It doesn't look like the value is automatically saved just based on if it's enabled or not.
You don't have to manually update the preference value if you are using PreferenceActivity.
For more information check following tutorials.
Click here
Click here
Related
I'm trying to retrieve the SwitchPreference's value using SharedPreferences but it isn't working. I'm using SwitchPreference so that user can turn on/off notifications, but it shows notifications no matter whatever the value is.
Here's the code.
NotificationUtils.java
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
if (preferences.getBoolean("notification_key", true)) {
notificationManager.notify(NOTIFICATION_ID + rowId, notBuilder.build());
}
preferences.xml
<SwitchPreference
android:contentDescription="Turn notifications on/off"
android:defaultValue="true"
android:key="notification_key"
android:summaryOff="Off"
android:summaryOn="On"
android:title="Notifications" />
I also have overridden and registered the OnSharedPreferenceChange listener in SettingsFragment.java.
Try replacing
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
with
PreferenceManager.getDefaultSharedPreferences(context);
I believe you're attempting to retrieve "notification_key" from the wrong SharedPreferences, which is why it's always using the default value of true and showing your notification.
Edit: You can check to see whether the SharedPreferences you're using contains the "notification_key" key with the contains() method.
I solved it, actually the value wasn't toggling, in settings screen the Switch was turning off and on but the value remained the default. Solved it by setting the new value in OnPreferenceChangeListener and that worked.
I've implemented a OnPreferenceChangeListener on two preference object in my preference page ( extends PreferenceActivity)
But ever since the preference value isn't updated upon change,
I even tried using :
SharedPreferences sharedPreferences = getSharedPreferences("myCustomSharedPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(preference.getKey(), Integer.parseInt(newValue.toString()));
editor.commit();
Where preference is the changed preference and newValue is the new value...
What am I missing ?
Thanks
You need to return true to get the value updated, see the Android documentation.
Can you see if preference.getKey() is giving the correct name? Can you print in your console and see. Code looks clean to me. May be the name is different than what is expected.
Also make sure that the value is also having the correct value.
I have modified the default android lock screen with some tweaks .
I have use a shared preference in the lock screen to show an overlay text when the phone is booting for the first time . I am getting the overlay in first boot and saving the shared preference value to false . And throughout that session i am able to read the value of the shared preference . But when i restart the phone it seems like the shared preference is resetting
private Boolean mShowOverlay;
private final String SHOW_OVERLAY = "showoverlay";
private SharedPreferences myPrefs ;
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
mShowOverlay = myPrefs.getBoolean(SHOW_OVERLAY, true);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putBoolean(SHOW_OVERLAY, false);
prefsEditor.commit();
Are you sure you are committing the SharedPreference, I mean calling commit() method??
I have an app which uses the user's location. I have a dialog(pic below) asking user's permission to "Allow" or "Disallow" the app to use the user's location ( dialog pops up the first time users opens the app after installation OR when user tries to use the location based service while using user location is "Disallow"-ed by the user).
I also use preference item(a checkbox)(pic below) in PreferenceActivity where the user can the toggle his preference.
To change the value of the sharedpreference I have use this code
public void onClick(DialogInterface dialog, int id)
{
sharedPrefs =getSharedPreferences("prefs",MODE_WORLD_WRITEABLE);
Editor editor = sharedPrefs.edit();
editor.putBoolean("locationPermission", true);
editor.commit();
}
I had expected the checkbox value to change automatically depending on the dialog selection as the key "locationPermission" holds the value to the checkbox. But it is not so.
Now how do I map the dialog(pic 1) selection to the checkbox value(pic 2)?
The issue was solved by using
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
instead of
sharedPrefs = getSharedPreferences("prefs", MODE_WORLD_WRITEABLE);
You can call addPreferencesFromResource in the onCreate of your PreferenceActivity so that your UI is populated from the preferences.
Also, you may want to make sure that your CheckBoxPreference has android:persistent-"true" in its XML definition.
I have a preferences activity with a checkbox "Enable Service".
I read the value like this :
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(con);
ServiceEnabled_Pref = prefs.getBoolean("EnableService", true);
ok, but how can I set this Preference ?
this is not a CustomShared Preference, It is a DefaultShared Preferences and it seems that there is not a method putBoolean for DefaultSharedPreferences.
I need this, cause I have a Widget with a button that needs to set this value to true/false
programatically:
prefs.editor().putBoolean("EnableService", true).commit();
however your best bet for defining the preferenceActivity and its defaults is via an xml file as decribed here with the android:defaultValue attribute
Use the shared preferences editor:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html