To edit sharedpreference value, we need editor to edit and save
the changes in shared preferences.

Editor editor =
pref.edit();

and to save data
commit() is used.

Editor.commit();

You can save data
into shared preferences using editor. All the primitive data types
like booleans, floats, ints, longs, and strings are supported. Call
editor.commit() in
order to save changes to shared preferences.

editor.putBoolean("key_name", true); // Storing boolean
- true/false

editor.putString("key_name", "string
value"); // Storing string

editor.putInt("key_name", "int
value"); // Storing integer

editor.putFloat("key_name", "float
value"); // Storing float

editor.putLong("key_name", "long
value"); // Storing long

editor.commit();
// commit changes

Get data from
Shared Preference:

Data can be
retrived from saved preferences by calling getString()
(For string) method.for boolean getBoolean() Remember this method
should be called on Shared Preferences not on Editor.

// returns stored
preference value

// If value is
not present return second param value - In this case null

pref.getString("key_name", null); // getting String

pref.getInt("key_name", null); // getting Integer

pref.getFloat("key_name", null); // getting Float

pref.getLong("key_name", null); // getting Long

pref.getBoolean("key_name", null); //
getting boolean

Delete data from
shared preference and delete sharedpreference:

To delete data
from shared preferences we can use remove(“key_name”).If
we want to delete all the data, call clear()