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,
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:layout_centerHorizontal="true"
- android:autoText="false" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save Data"
- android:id="@+id/saveDataButton"
- android:layout_below="@+id/editText"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Load Data"
- android:id="@+id/loadDataButton"
- android:layout_below="@+id/saveDataButton"
- android:layout_centerHorizontal="true" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:id="@+id/outText"
- android:layout_below="@+id/loadDataButton"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Now bind the controls in the onCreate(),
- editText = (EditText) findViewById(R.id.editText);
- Button loadDataButton = (Button) findViewById(R.id.loadDataButton);
- Button saveDataButton = (Button) findViewById(R.id.saveDataButton);
- output = (TextView) findViewById(R.id.outText);
- loadDataButton.setOnClickListener(loadDataListner);
- saveDataButton.setOnClickListener(saveDateListner);
And also initialise the shared preference here,
- 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,
- SharedPreferences.Editor editor = preferences.edit();
- 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(),
For loading value from shared preference we need to use the following,
- 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,
- View.OnClickListener loadDataListner = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
-
- String data = preferences.getString("text",null);
- Log.e("DATA______", "" + data);
- if(TextUtils.isEmpty(data)){
- output.setTextColor(Color.RED);
- output.setText("No data found...");
- }else{
- output.setTextColor(Color.BLUE);
- output.setText(data);
- }
- }
- };
- View.OnClickListener saveDateListner = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SharedPreferences.Editor editor = preferences.edit();
- editor.putString("text", editText.getText().toString());
- editor.commit();
- Toast.makeText(MainActivity.this,"Data Saved.",Toast.LENGTH_SHORT).show();
- editText.setText("");
- }
- };
Please see the screen shots also