This question already has answers here:
How to use SharedPreferences in Android to store, fetch and edit values [closed]
(30 answers)
Closed 9 years ago.
I am trying for the last hour to save an integer in my Android application. I read that this can be done using the SharedPreferences. However i dont understand why it seems so confusing to do so.
How can i simply save an int variable ? And then when i run the application again , how can i interact with this variable again?
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();
the you can get it as:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("your_int_key", -1);
The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API
public void SaveInt(String key, int value){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public void LoadInt(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
savedValue = sharedPreferences.getInt("key", 0);
}
If you want to save the variable somewhere, you have to write SaveInt("key", 5); With this you will save the value 5, while the first default value is 0. If you want to load it and use it in another activity, you have to write both of these methods there, and call LoadInt(); where you need the variable. The savedValue is a predefined integer (this needs to be declared everywhere you would like to use the saved variable)
This is the example of setting Boolean preferences. You can go with Integer also.
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
Hope this might be helpful.
Related
I am creating my first android app and I don’t have enough knowledge in programming unlike other here. So please help me with this matter. I want to save player’s name and score. I’ve searched and read a lot about SharedPreference but was unable to understand it very well especially when I am already applying the codes on my activity. I have tried it twice and everytime I run it, it shows”Not Responding” message and closes. But when the codes for sharedpreference are not indicated there it works fine. So here’s how I want to do with these. On the first game, the very first score should be saved after the time is over (the user will be typing their name and click save). So when another user or player plays the game the Top Score should be indicated in the screen using a textview, below this Top Score textview is the Current Score textview. If the Current Score is greater than the Top Score then it should overwrite the score. If not then the Top Score should stay the same. How can I achieve this?
P.S. I have 3 different activities that needs this SharedPreferences but with the same functionalities. Thanks in advance. And please explain it more clearly (coz sometimes I can’t understand some words) thank you :)
This is the code i use, i put it in Oncreate
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name);
editor.putInt("score", score);
editor.commit();
I think following link will surely help you out SharedPreferences Explained in detail
.
Try this
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, value); //similar way you can push integer values
editor.commit();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sp.getString(key, null); //default value will be null in this case, if there is no such key
You can write a Utility class to reuse this methods wher you want. like below
public class PreferenceUtility {
public static void saveString(Activity activity, String key, String value){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, value);
editor.commit();
}
public static String readString(Activity activity, String key, String defaultValue){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
return sp.getString(key, defaultValue);
}
}
static SharedPreferences sh_Pref;
public static String Preference_Name = "AppData";
public static String getPreference(String key, String Default,
Activity activity)
{
sh_Pref = activity.getSharedPreferences(Preference_Name, Context.MODE_PRIVATE);
return sh_Pref.getString(key, Default);
}
public static void setPreference(String key, String value, Activity activity)
{
if (value != null)
{
sh_Pref = activity.getSharedPreferences(Preference_Name, Context.MODE_PRIVATE);
Editor editor = sh_Pref.edit();
editor.putString(key, value);
editor.commit();
}
}
}
** Here a set and get value from SharedPreferences**
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","");
I have got a SharedPreferences file in my Activity1.
void saveDays(){
Log.w(TAG, "Start saveDays");
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("Days", days);
editor.commit();
}
Then I need to use days in my Activity2. So how can I load it?
In Activity2, where you want to get access to that preference:
int days = getPreferences(MODE_PRIVATE).getInt("Days", DEFAULT_DAY);
Where DEFAULT_DAY is the default value to use if there is no "Days" preference available.
You want to name the preferences and access them with that name in both
SharedPreferences prefs =
getSharedPreferences("myPrefs", MODE_PRIVATE);
And then its as simple as retrieving the int...
int days = prefs.getInt("Days", 1);
I am trying to save data using default Shared Preferences, but cannot get this code working.
I cant retrieve an int value in another activity.
In my first activity, in onCreate, I want to create a scorecounter that counts up every time you receive points. It will be initiated on first run.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
if (preferences.getBoolean("first_run", true))
{
editor.putInt("totalpoang", 0);
editor.commit();
preferences.edit().putBoolean("first_time", false);
}
In my Activity B I want to retrieve the value, modify it, and then save it in preferences again.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int poang = preferences.getInt(totalpoang, 0);
editor.putInt("antalpoang", totalpoang);
editor.commit();
However I only get the error totalpoang cannot be resolved as a variable.
I initialized it as a int in the first Activity, and I am trying to retrieve it as an int.
What am I doing wrong?
Change this
int poang = preferences.getInt(totalpoang, 0);
//editor.putInt("totalpoang", 0); // key is "totalpoang"
to
int poang = preferences.getInt("totalpoang", 0);
And then
editor.putInt("antalpoang", poang);
I'm just starting out.
I'm trying to increment a simple counter, every time I run the project on the emulator.
I thought adding an integer type item in strings.xml would help, but that's final, and can't be modified.
Basically I'd just display in my app's first basic screen:
Started: N where N would be the Nth time I've launched the project from Eclipse.
How can I make such a counter that's persistent across application launches and exits?
Got it:
SharedPreferences pref = getPreferences(MODE_PRIVATE);
int tempN=pref.getInt("N", 0);
tempN++;
SharedPreferences.Editor editor = pref.edit();
editor.putInt("N",tempN);
editor.commit();
msgBox.setText("Started:"+tempN);
One thing I still don't understand, that when I call pref.getInt("N",0), is the key-value pair <N,0> automatically created?
You can use shared preference for that . You can store integer number in shared preference and get value of it when ever you want.
Try this.
SharedPreferences prefs = getSharedPreferences("Share", Context.MODE_PRIVATE );
Editor editor = prefs.edit();
editor.putInt("Value", 1 );
editor.commit();
for get value
prefs.getInt("Value",0);
In your main activity onCreate():
SharedPreferences pref = this.getSharedPreferences();
int count = pref.getInt("your key", 0) //0 is default value.
count++;
SharedPreferences.Editor edit = pref.edit();
edit.putInt("your key", count);
edit.commit();
// display current count