android editing shared preferences

by Prakash 2014-03-31 16:22:07

Step 1:
Declare class variables :


// Shared Preferences
SharedPreferences pref;

// Editor for Shared preferences
Editor editor;

// Sharedpref file name
private static final String PREF_NAME = "EluthuPref";

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";

// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";



Step 2:

In the constructor, do as follow :


// Constructor
public MyClassname(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}



Step 3:

Use the edit function as follow :



/**
* Edit shared preferences
* */
public void editMyPreferences(){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);


// commit changes
editor.commit();
}


This way using an editor you can edit the shared preferences in Android.
590
like
0
dislike
0
mail
flag

You must LOGIN to add comments