Reading shared preferences - android

i'm using shared preferences for the settings menu of my android app.
it's working very well but i didn't know how to use these settings on my code:
For example how to use the selected language and use it in another activity:
<PreferenceCategory
android:title="General Settings"
android:key="general_settings"
>
<ListPreference
android:key="language"
android:title="Language"
android:summary="Define the default language"
android:defaultValue="Spanish"
android:entries="#array/Languages"
android:entryValues="#array/LanguagesValues"
/>

On code behind;
SharedPreferences prefs = this.getSharedPreferences("general_settings", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("language", null);

you have to set and read shared pref settings. For example:
Set:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("language", language);
editor.commit();
Read:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String language = settings.getString("language", "");
You could use a RadioGroup, set the Sharedpref and work with it.
Hope this help!

Related

How do I use sharedPreferences in Android?

I have an Android app which in one of the activities the user check one of the radio buttons. I want to save the user's choice and use its value in another activity.
To store
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("KEY", your_boolean).commit();
To retrieve
Boolean your_boolean = prefs.getBoolean("KEY", false);
You can use shared preference for saving the values.
For Saving value into SharedPreferences use below code
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
"SHARED_PREF", MODE_PRIVATE).edit();
editor.putString("radio_value", value);
editor.commit();
For Retriving value from SharedPreferences use below code
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"SHARED_PREF", MODE_PRIVATE);
String storedValue = prefs.getString("radio_value","");

Shared Preferences in not being cleared,

I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);

Storing and retrieving shared preferences in android

I am trying to store some settings using preferences I am using this code:
SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE);
pref.edit().putString("some settings", "lalal");
pref.edit().commit();
What i am doing wrong the file gets created but is empty
Try this code:
SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putString("some settings", "lalal");
editor.commit();
and get values using this code:
SharedPreferences shf = getSharedPreferences("myCustomSharedPrefs", MODE_WORLD_READABLE);
String strShPref = shf.getString("some settings", "");
You've got 2 different editors for your prefs, first one adds the string, and second one commits empty changes, because you've changed another editor.
Change this:
pref.edit().putString("some settings", "lalal");
pref.edit().commit();
into this:
pref.edit().putString("some settings", "lalal").commit();

How do I change the value of a SharedPreference without going into the preference screen?

I have a string I want to store in my SharedPreferences. Is there some kind of setString I could do to accomplish this?
You mean something like this?
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "myUsername");
editor.commit();
You can make changes in the SharedPreferences, and commit the changes after you are done.
Try this.
SharedPreferences prefs = getSharedPreferences("PreferenceFileName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("StringNameToBeStored", "value");
editor.commit();
And you can do everything in the same line, there's no need to declare editor
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("username", "myUsername").commit();
And don't forget the commit!

how to empty the sharedpreferences storage in android?

i'm using sharedPreferences to store username and password .. but i need to make log off, how can i remove the data od username and password ?
You have to use the SharedPreferences Editor object to do it. You can follow the next example:
SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("username_field");
editor.remove("password_field");
editor.commit();
Just in case you want to clear the entire preference too, useful if you've only got the users details in the preference, and you want to remove all of that, you Can use.
SharedPreferences preferences =
getSharedPreferences("PREFERENCE",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

Categories

Resources