Shared Preference In Android

Shared Preference in Android

In Android there are some ways to store data in the phone permanently, for example we can store the date using Data base, File or Shared Preference. Here I am going to describe how we can store and retrieve data using shared preference. Shared preference is a key value pair storing. In this we can store each value using a key. For retrieving that particular value we need to give that key. For using shared preference we need to initialize this with a mode. There are some modes available for shared preference, some of them  are as follows
  • MODE_APPEND - which is used to append values to an already existing preferences
  • MODE_PRIVATE - Settings this mode will allows only the same application to use this preferences
  • MODE_WORLD_READABLE - This will allows other applications to read this preference
  • MODE_WORLD_WRITABLE - This will allows other application to write the preferences
In this article I am going to describe an application which has two buttons named Save Data and Load Data: an edit text that the user can give texts that he wants to save, and a text view which will show the status of loading texts from shared preferences.
 
For that I am creating my layout file first,
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.     <EditText  
  8. android:layout_width="match_parent"  
  9. android:layout_height="wrap_content"  
  10. android:id="@+id/editText"  
  11. android:layout_centerHorizontal="true"  
  12. android:autoText="false" />  
  13.     <Button  
  14. android:layout_width="wrap_content"  
  15. android:layout_height="wrap_content"  
  16. android:text="Save Data"  
  17. android:id="@+id/saveDataButton"  
  18. android:layout_below="@+id/editText"  
  19. android:layout_centerHorizontal="true" />  
  20.     <Button  
  21. android:layout_width="wrap_content"  
  22. android:layout_height="wrap_content"  
  23. android:text="Load Data"  
  24. android:id="@+id/loadDataButton"  
  25. android:layout_below="@+id/saveDataButton"  
  26. android:layout_centerHorizontal="true" />  
  27.     <TextView  
  28. android:layout_width="wrap_content"  
  29. android:layout_height="wrap_content"  
  30. android:textAppearance="?android:attr/textAppearanceSmall"  
  31. android:id="@+id/outText"  
  32. android:layout_below="@+id/loadDataButton"  
  33. android:layout_centerHorizontal="true" />  
  34. </RelativeLayout>  
Now bind the controls in the onCreate(),
  1. editText = (EditText) findViewById(R.id.editText);  
  2. Button loadDataButton = (Button) findViewById(R.id.loadDataButton);  
  3. Button saveDataButton = (Button) findViewById(R.id.saveDataButton);  
  4. output = (TextView) findViewById(R.id.outText);  
  5. loadDataButton.setOnClickListener(loadDataListner);  
  6. saveDataButton.setOnClickListener(saveDateListner);  
And also initialise the shared preference here,
  1. preferences = getSharedPreferences("MyActivity",0);  
The first parameter is the name for that preference and 0 indicates the MODE_PRIVATE. For adding values to shared preference we need to use the editor by using the following lines of code,
  1. SharedPreferences.Editor editor = preferences.edit();  
  2. editor.putString("text", editText.getText().toString());  
Here text is the key for that value stored in the shared preferences, for saving this to shared preference we need to call the commit(),
  1. editor.commit();  
For loading value from shared preference we need to use the following,
  1. String data = preferences.getString("text",null);  
here text is the key and null is the default value for the preference.
 
Please see the saveDate listner function and loaddata listner functions as follows,
  1. View.OnClickListener loadDataListner = new View.OnClickListener() {  
  2. @Override  
  3. public void onClick(View view) {  
  4.   
  5.         String data = preferences.getString("text",null);  
  6.         Log.e("DATA______""" + data);  
  7. if(TextUtils.isEmpty(data)){  
  8. output.setTextColor(Color.RED);  
  9. output.setText("No data found...");  
  10.         }else{  
  11. output.setTextColor(Color.BLUE);  
  12. output.setText(data);  
  13.         }  
  14.     }  
  15. };  
  16. View.OnClickListener saveDateListner = new View.OnClickListener() {  
  17. @Override  
  18. public void onClick(View view) {  
  19.         SharedPreferences.Editor editor = preferences.edit();  
  20.         editor.putString("text", editText.getText().toString());  
  21.         editor.commit();  
  22.         Toast.makeText(MainActivity.this,"Data Saved.",Toast.LENGTH_SHORT).show();  
  23. editText.setText("");  
  24.     }  
  25. };  
Please see the screen shots also
 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all