<p>There are many ways of storing data in android, One of the simplest way is SharedPrefrences If you have a relatively small collection of key-values to save, you should use the<span style="font-family: monospace;"> SharedPrefrences</span> APIs, Using SharedPrefrences You can <span style="color: #000000;">stores</span>: <span style="color: #008000;"><strong>String, Integer, Float, Long, Double and a String Set. </strong></span></p>
<p>download the full project from <strong><a href="https://github.com/arunk7839/SharedPreference">Github.</a></strong></p>
<h4><span style="color: #000080;">Save data to shared preferences</span></h4>
<pre><code>SharedPreferences sharedPreferences=getSharedPreferences("AppName",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();

editor.putString("String","string");
editor.putBoolean("bool",true);
editor.putFloat("float",1.0f);
editor.putInt("int",1);
editor.putLong("long",1);

editor.apply();</code></pre>
<p><span style="color: #000080;"><strong>Parameter:</strong></span></p>
<p><span style="color: #008000;"><strong>&#8220;appName&#8221;</strong></span><br />
is name of the SharedPreference which will be useful when you read data from shared<br />
preferences.</p>
<p><span style="color: #008000;"><strong>&#8220;MODE_PRIVATE&#8221;</strong></span><br />
By setting this mode, the file can only be accessed using by only your application.</p>
<p><span style="color: #008000;"><strong>&#8220;Stringkey&#8221;</strong></span><br />
is the Key for save and retrieve the value.</p>
<p><span style="color: #008000;"><strong>apply()</strong></span> or <span style="color: #008000;"><strong>commit()</strong></span> to save the changes after putting data into the editor.</p>
<h4><span style="color: #000080;"><strong>Difference between apply() and commit():</strong></span></h4>
<p>apply() write the data to disk asynchronously and commit write the data<br />
synchronously We should avoid calling the commit() from main thread because<br />
it could pause UI rendering.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h4><span style="color: #000080;"><strong>Read from shared preferences</strong></span></h4>
<pre><code>SharedPreferences sharedPref = getSharedPreferences("AppName",MODE_PRIVATE);
String stringValue=sharedPref.getString("string","");</code></pre>
<p><span style="color: #000080;"><strong>Parameters:<br />
</strong></span><span style="color: #008000;">StringKey</span> is the key for getting the value.<br />
optionally a default value to return if the key isn&#8217;t present.</p>
<h4><span style="color: #000080;">Full code:</span></h4>
<pre><code>package c1c.sharedpreferenceexp;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 //Save the data in shared Preference.
 SharedPreferences sharedPreferences=getSharedPreferences("AppName",MODE_PRIVATE);
 SharedPreferences.Editor editor=sharedPreferences.edit();

 editor.putBoolean("bool",true);
 editor.putFloat("float",1.0f);
 editor.putInt("int",1);
 editor.putLong("long",1);
 editor.putString("String","string");

 Set<;String>; set=new HashSet<;String>;();
 set.add("element1");
 set.add("element2");
 set.add("element3");
 set.add("element4");
 editor.putStringSet("stringSet",set);
 editor.apply();

 //get the data from shared Prefernces.
 SharedPreferences sharedPref = getSharedPreferences("AppName",MODE_PRIVATE);

 boolean bool=sharedPref.getBoolean("bool",false);
 Log.e("bool ",""+bool);

 float floatValue=sharedPref.getFloat("float",0);
 Log.e("floatValue ",""+floatValue);

 int intValue=sharedPref.getInt("int",0);
 Log.e("int ",""+intValue);

 long longValue=sharedPref.getLong("long",0);
 Log.e("long ",""+longValue);

 String stringValue=sharedPref.getString("string","");
 Log.e("stringValue ",""+stringValue);

 Set<;String>; setvalue=sharedPref.getStringSet("stringSet",null);
 Log.e("setValue ",""+setvalue);
 }
}</code></pre>


