Set the value SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE); Editor editor = pref.edit(); editor.putString("Vol", SGetNumVol); editor.putString("Comment", SGetComment); editor.apply(); Get the value in another Class SharedPreferences prefs = getSharedPreferences("MyPref", Context.MODE_PRIVATE); String vol = prefs.getString("Vol", "No name defined"); String comment = prefs.getString("Comment", "No name defined");
private void setLoginData(String mobile, String pin, String emailId) { SharedPreferences pref = getActivity().getSharedPreferences("LoginInfo", 0); Editor editor; editor = pref.edit(); editor.putString("mobileNumebr", mobile); editor.putString("pin", pin); editor.putString("emailId", emailId); editor.putBoolean("login",true); editor.apply(); }
SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); Editor editor = sharedPreferences.edit(); editor.putString(key, name); editor.apply();
SharedPreferences settings; Editor editor; settings = c.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE); editor = settings.edit(); Gson gson = new Gson(); String ItemsJson = gson.toJson(list); editor.putString(categoryId, ItemsJson); editor.apply();
SharedPreferences prefs = context.getSharedPreferences("my-contacts", Context.MODE_PRIVATE); // Get contacts String jsonString = prefs.getString("contacts", "[]"); JSONArray json = new JSONArray(jsonString); // You can now iterate, remove, add contacts to this json array // Store contacts Editor edit = prefs.edit(); edit.putString("contacts", json.toString()); edit.apply();
SharedPreferences sharedpreferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE); Editor editor = sharedpreferences.edit(); editor.putString("user_logged_in", "true"); editor.apply();
int id = selectedItemNum; ArrayList<String> list = new ArrayList<String>(); list.add("Item1"); list.add("Item2"); list.add("Item3"); list.add("Item4"); String selectedString = list.get(id); String APP_PREFERENCES = "savedStrings"; SharedPreferences mySharedPreferences = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); Editor editor = mySharedPreferences.edit(); editor.putString("savedString"+id, selectedString); editor.apply();
//Get Data SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity()); String url = settings.getString("url", "n/a"); //Save Data Editor edit = preferences.edit(); edit.putString("username", "new_value_for_user"); edit.apply();
SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); Editor editor = sharedPreferences.edit(); editor.putString(key, name); editor.apply();
//Some String that I should remember, I am just using the package name for now String app = this.getPackageName();/*This is going to be used more like a file to save my stuff to*/ //Setting our sharedpreferences SharedPreferences sha = sha = getApplicationContext().getSharedPreferences(app, SherlockActivity.MODE_PRIVATE); String myString = "This is the String that you want to save so you can use among your classes" //Now we call in an editor for that SharedPreferences so we can write and delete stuff from it . Editor edit = sha.edit(); //Now we insert our String. edit.putString("Something_you_can_remember" , myString);//You will need the "Something_you_can_remember" a few lines ahead , so remember it ! edit.apply(); //Or we can use edit.commit() , but I prefer apply() //Now our String is saved ! So lets read it ! String whatever = sha.getString("Something_you_can_remember" , "The String incase myString didn't even exist , saves you from a NullPointerException"); //Here we go ! Now we have our String saved and can be readable among the classes !
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Editor editor = preferences.edit(); editor.putString("LAST_SMOKE_DATE", new Date().toString()); editor.apply();
Editor edit = preferences.edit(); edit.putString("isPayed", "yes"); edit.apply();
Editor editor = preferences.edit(); editor.putString(ID_KEY,userID); editor.apply();
public static final String MyPreferences = "MyPrefs" ; SharedPreferences sharedPreferences; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { sharedPreferences = getSharedPreferences(MyPreferences, Context.MODE_PRIVATE); editor = sharedPreferences.edit(); } //For Save Value (But the value is string) public void saveValue(String code, String value) { editor.putString(code, value); //Dont forget this method must be typed, too... editor.apply(); } //For Get Value (But the value is string) public String getValue(String code) { return sharedPreferences.getString(code, ""); }
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(YourActivity.this); Editor editor = sp.edit(); editor.putString("patientMobile", patientMobile); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { editor.apply(); } else { editor.commit(); }
editor.putString(key, theURL); editor.apply();
editor.putString(key, value);
//Some String that I should remember, I am just using the package name for now String app = this.getPackageName();/*This is going to be used more like a file to save my stuff to*/ //Setting our sharedpreferences SharedPreferences sha = sha = getApplicationContext().getSharedPreferences(app, SherlockActivity.MODE_PRIVATE); String myString = "This is the String that you want to save so you can use among your classes" //Now we call in an editor for that SharedPreferences so we can write and delete stuff from it . Editor edit = sha.edit(); //Now we insert our String. edit.putString("Something_you_can_remember" , myString);//You will need the "Something_you_can_remember" a few lines ahead , so remember it ! edit.apply(); //Or we can use edit.commit() , but I prefer apply() //Now our String is saved ! So lets read it ! String whatever = sha.getString("Something_you_can_remember" , "The String incase myString didn't even exist , saves you from a NullPointerException"); //Here we go ! Now we have our String saved and can be readable among the classes ! //Also , if you wanted to delete that String or whatever you "put" in there , you can call edit.remove("Something_you_can_remember"); //or edit.clear() to remove all the values stored !
public class MySharedPreferences{ private static final String USER_PREF = "user_shared_pref"; private static final String DEVICE_PREF = "device_shared_pref"; private static SharedPreferences getSharedPreferences(Context ctx){ return PreferenceManager.getDefaultSharedPreferences(ctx); } public static void setUserPref(Context ctx,String user_pref){ Editor editor = getSharedPreferences(ctx).edit(); editor.putString(USER_PREF,user_pref); editor.apply(); } public static String getUserPref(Context ctx){ return getSharedPreferences(ctx).getString(USER_PREF,""); } }