How to save and fetch integer value in Shared Preference in android?

how to get data from shared preference in android
how to use shared preference in android between activities
shared preference login example in android
how to set and get value in sharedpreferences in android
android login with database using shared preferences
how to save and load sharedpreferences in android
sharedpreferences - android example
how to store username and password in sharedpreferences in android

I want to save and fetch the static integer value of Snow Density in Shared Preferences and change when user select another value in the Single choice. My Code :

public static int mSnowDensity;
AlertDialog.Builder mABuilder = new AlertDialog.Builder(AAA.this);
final CharSequence mCharSequence[] = { "Low", "Medium", "High" };
mABuilder.setTitle("Set Density of Snow");
mABuilder.setSingleChoiceItems(mCharSequence,
        WallpaperServices.mDensitySnow,
        new android.content.DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                if (which == 2) {

                    mSnowDensity = 90;
             /*I Want to save mSnowDensity Value In Shared Preferences */
                } else if (which == 1) {

                     mSnowDensity = 60;
                } else {

                     mSnowDensity = 30;
                }

                dialog.dismiss();
            }
        });

You can use shared preferences as follows

//To save
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY",mSnowDensity);
editor.commit();

//To retrieve
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0); //0 is the default value

getSharedPreferences() is a method of the Context class. If you are inside a Activity or a Service (which extend Context) you can use it like in this snippet. Else you should get the context using getApplicationContext() and then call getSharedPreferences() method.

For more options you can refer to the documentation at http://developer.android.com/guide/topics/data/data-storage.html#pref

How to save and load data via Shared Preferences in Android, Shared Preferences allow you to save and retrieve data in the form of key, value pair. It will save a integer value in a preference editor. Read from shared preferences. To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

To save in the SharedPreferences:

private final String PREFS_NAME  = "filename";
private final String KEY_DENSITY    = "den";

Context  ctx   = getApplicationContext();
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putInt(KEY_DENSITY, mSnowDensity);
editor.commit();

To get the value:

Context ctx = getApplicationContext();
String strSavedValue = null;
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

strSavedValue = sharedPreferences.getInt("den", anyDefaultValue);

How to save and fetch integer value in Shared Preference in android , I want to save and fetch the static integer value of Snow Density in Shared Preferences and change when user select another value in the Single choice. It will save a long value in a preference editor. putInt (String key, int value). It will save a integer value in a preference editor. putFloat (String key, float value).

Save the value in prefrence

private void SavePreferences(String key, int value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

get the value from preference

 private void showPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        int savedPref = sharedPreferences.getInt(key, 0);

       }

You can use the key as the shared preference name

Save key-value data, A SharedPreferences object points to a file containing key-value pairs to use the SharedPreferences APIs to store and retrieve simple values. In android, Shared Preferences are used to save and retrieve the primitive data types (integer, float, boolean, string, long) data in the form of key-value pairs from a file within an apps file structure.

Initialization We need an editor to edit and save the changes in shared preferences. The following code can be used to get the shared preferences.

 SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 :- for private mode
Editor editor = pref.edit();

Storing Data

editor.putBoolean("key_name", true); 
editor.putString("key_name", "string value"); 
editor.putInt("key_name", "int value"); 
editor.putFloat("key_name", "float value");
editor.putLong("key_name", "long value");  
editor.commit(); 

Retrieving Data

pref.getString("key_name", null); // getting String
pref.getInt("key_name", -1); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

Clearing or Deleting Data

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email

editor.commit(); // commit changes


editor.clear();
editor.commit(); // commit changes

android.content.SharedPreferences.getInt java code examples , //--Init int myvar = 12; //--SAVE Data SharedPreferences preferences = context. getString("name", "No name defined");//"No name defined" is the default value. int idName = prefs.getInt("idName" Retrieve an int value from the preferences. getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences. getInt(String key, int defValue): This method is used to retrieve an int value from the preferences. getLong(String key, long defValue): This method is used to retrieve a long value from the preferences.

Saving preference is not a troubling task. But if you have a lot of such configurable options you could use a PreferenceActivity and override onSharedPreferenceChanged.

More details here http://developer.android.com/guide/topics/ui/settings.html

Shared Preferences in Android with Examples, Shared Preferences can be thought of as a dictionary or a key/value pair. For example, when the user's settings need to be saved or to store data that can be getInt(String key, int defValue): This method is used to retrieve an int value from  putLong(String key, long value) It will save a long value in a preference editor. 5: putInt(String key, int value) It will save a integer value in a preference editor. 6: putFloat(String key, float value) It will save a float value in a preference editor

Android SharedPreferences Basics, SharedPreferences,SharedPreferences example,Android user sessions, session Management,Basics,example You can save and retrieve key, value pair data from Shared preferences. putInt( "key_name2" , "int value" ); // Saving integer. Android Shared Preferences Overview. Shared Preferences allows activities and applications to keep preferences, in the form of key-value pairs similar to a Map that will persist even when the user closes the application.

Android Shared Preferences Example Tutorial, In this tutorial we'll use Shared Preferences in our android application to store data in the form of putInt("key_name", "int value"); // Storing integer editor. The MainActivity.java file is used to save and retrieve the data through keys. package  In Shared Preferences save data to shared preference by calling the getSharedPreferences () method. The SharedPreferences Editor is used to apply the changes which we have made while storing the data. The data is saved using commit () method.

9.1: Shared Preferences · GitBook, The shared preferences file itself is managed by the Android framework, and accessible to only when you need to save a small amount data as simple key/​value pairs. Add the ability to save, retrieve, and reset shared preferences to an app. Both the count and color integers are saved to the instance state bundle in​  Today we will learn how to use Shared Preferences in Android to store data. We will use a SharedPreferences.Editor to save Strings and Booleans, but you can also save other data types like

Comments
  • Check this PrefUtils class gist.github.com/john1jan/b8cb536ca51a0b2aa1da4e81566869c4
  • How do you get those without context from anywhere?
  • @g00dy - By default it uses the context associated with the activity, the same which you get using getBasecontext()
  • by default yes, but what happens if you call it ouside of the Activity?
  • In that case you get the context as you described ;)
  • So, edit your answer, so that if it gets accepted and somebody at some point sees it - the answer can be correct when pasted into any given place :)