i need to store a variable in phone memory, and later get that variable to show it.
Please, give me some information or anything to search. Thanks
static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
editor.putString("Variablenname_1", "1");
editor.commit();
if you want so get the value use:
String val = settings.getString("Variablenname_1", "0");
I think Shared Preferences is a good idea for you, read this for more information: http://developer.android.com/guide/topics/data/data-storage.html
The final result.
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String text = app_preferences.getString("name", "default");
...
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("name",name.getText().toString());
editor.commit();
Related
I try Shared preferences for the first time and I am facing a problem. When i set my Shared Preferences:
SharedPreferences sp = getSharedPreferences();
The getSharedPreferences is red and there is also no suggestion from android studio to change it.
Do I have to update my gradle file or is that function replaced by something else?
You have to call .getSharedPreferences() on the current context (for example getActivity().getPreferences(Context.MODE_PRIVATE);).
You can see some examples here: https://developer.android.com/training/data-storage/shared-preferences.
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", "string value"); // Storing string
editor.commit();
Get String from your SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String newval = pref.getString("key_name", null);
You should use context like below
private val sharedPreferences: SharedPreferences? =
context.getSharedPreferences("Preference", Context.MODE_PRIVATE)
The below code helped me:
SharedPreferences sh = appCompatActivity.getApplicationContext().getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
I am using following code:
SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.
Why is that?
editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.
I have a problem with SharedPreferences in Android.
This is my code:
SharedPreferences s = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
s.edit().putString("eur", "1.80");
s.edit().commit();
SharedPreferences a = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
String kurs = a.getString("eur","7");
Toast hhh= Toast.makeText(getApplicationContext(),kurs, Toast.LENGTH_LONG);
hhh.show();
I´m setting the String and want to read it out directly after that in the onCreate method. But i always get the specified default value "7".
What was wrong? I already researched for that problem, but i can´t found helpful things.
Thanks for your help :)
Each time you call "s.edit()" a new editor is created. Thus your "commit()" call is on an instance of the editor that has not had your setting applied. Try this:
SharedPreferences s = this.getSharedPreferences("kurs",MODE_WORLD_READABLE);
Editor editor = s.edit();
editor.putString("eur", "1.80");
editor.commit();
Please try my code below. What i think is wrong in your code, that you are using different "Editor" instances here:
"s.edit().putString("eur", "1.80");"
and here
s.edit().commit();
private static String APP_SHARED_PREFS = "MyAppID";
// Write the value
SharedPreferences.Editor prefsEditor = getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE).edit();
prefsEditor.putString("KEY", "VALUE");
prefsEditor.commit();
// Get the value
return getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE).getString("KEY", "");
SharedPreferences myPrefs = this.getSharedPreferences("kurs", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("eur", "1.80");
// commit the edits
editor.commit();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", context.MODE_WORLD_READABLE);
String kurs = myPrefs.getString("eur", "7");
Toast hhh= Toast.makeText(getApplicationContext(),kurs, Toast.LENGTH_LONG);
hhh.show();
Try This
I am trying to use a value stored in a shared preference to help style a listview and when I use this code it comes back the default value;
SharedPreferences pref = context.getSharedPreferences("Level", 0);
mCounter = pref.getInt("Level", 3);
This is the code I used to store the preference:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
pref.edit().putInt("Level", 1).commit();
getSharedPreferences(String name, int mode)
where name is the name of the preferences file
getInt (String key, int defValue)
where key is the actual preference key
Do you have named your preferences and the key both "Level"? If not that is the problem.
This should work:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
mCounter = pref.getInt("Level", 3);
Are you sure the preferences have been changed from their default settings? When using the SharedPreferences.Editor you must remember to call commit() to save your changes.
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
mCounter = pref.getInt("Level", 3);
You have to do above code..
Use below code to store & get value from prefrance. this is best way to do this.
For store data:
pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editPref = pref.edit();
editPref.putBoolean("logedin", true);
editPref.commit();
For Get Data:
pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editPref.getBoolean("logedin", false);
You need to set same shared preference name when you want to get the value.Like here you want to get your value in "Level" preference but put in different shared preference " com.komodostudios.asllessons".
So that you get the default value for "Level"
Code will be like that:
To store:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
Editor editPreference=pref .edit();
editPreference.putInt("Level",2);
editPreference.commit();
To retrieve:
SharedPreferences pref = getSharedPreferences("com.komodostudios.asllessons", MODE_PRIVATE);
int mCounter = pref.getInt("Level",1);
In my android app I have
SharedPreferences usernamePref = context.getSharedPreferences(USERNAME_PREF, context.MODE_PRIVATE);
String username = usernamePref.getString(USERNAME_PREF, ERROR);
SharedPreferences emailPref = context.getSharedPreferences(EMAIL_PREF, context.MODE_PRIVATE);
String email = emailPref.getString(EMAIL_PREF, ERROR);
SharedPreferences passwordPref = context.getSharedPreferences(PASSWORD_PREF, context.MODE_PRIVATE);
String password = usernamePref.getString(PASSWORD_PREF, ERROR);
The reason this is confusing me is because the key is being used twice, which seems redundant. So I am wondering, is it possible to just get all the preferences from a single SharedPreferences object?
This is the way I typically do it:
PreferenceManger prefManager = PreferenceManager.getDefaultSharedPreferences(context);
String username = prefManager.getString(USERNAME_PREF);
String email = prefManager.getString(EMAIL_PREF);
String password = prefManager.getString(PASSWORD_PREF);
Note: You will want to be editting the single SharedPreferences for your app, not individual as you have it now. To edit it this way do the following:
PreferenceManger prefManager = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefManager.edit();
editor.putString(USERNAME_PREF, usernameString);
editor.putString(EMAIL_PREF, emailString);
editor.putString(PASSWORD_PREF, passwordString);
editor.commit();
How are you storing the preferences? It would make sense to use a single SharedPreferences.
Just use the editor on for one SharedPreferences and use putString with your different keys.