Pr.Pg Next Pg

Shared Preferences Tutorials

 

Introduction

  • We will now go more into depth of Shared Preferences.

  • We have already made an application which will store our preferences but what is we want to store Data and other things of our application.

  • Shared Preferences store small amount of data and it is private to your application.

  • The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.

  • You can use Shared Preferences to save any primitive data: booleans, floats, ints, longs, and strings.

  • This data will persist across user sessions (even if your application is killed).

  • Application Example

  • We will illustrate the use of Shared Preferences with an application example and also explain various methods used to store and retrieve data.

  • We create an XML layout which has two Buttons and a Edit Text and a Text View.

  • In the Edit Text we will type the String which we will be saving on clicking the Save button and when we click the Load button the text will be retrieved.

  • Similarly we can save many types of data as mentioned above.

  • Below is the XML code of the file.

 

sharedprefs.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

 

<EditText

android:id="@+id/etData"

android:layout_width="match_parent"

android:layout_height="wrap_content"

</EditText>

 

<Button

android:id="@+id/bSaveData"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Save" />

 

<Button

android:id="@+id/bLoadData"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Load" />

 

<TextView

android:id="@+id/tvData"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center"

android:gravity="center"

android:text=""

android:textAppearance="?android:attr/textAppearanceLarge" />

 

</LinearLayout>

 

 

  • Below is the code of the java file of the activity.

 

SharedPrefs.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class SharedPrefs extends Activity implements OnClickListener{

Button bload;

Button bsave;

EditText etData;

TextView tvData;

staticpublic String filename = "MyFileOfData";

SharedPreferences sp;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.sharedprefs);

setupVars();

sp = getSharedPreferences(filename, 0);

}

private void setupVars() {

// TODO Auto-generated method stub

bload =(Button)findViewById(R.id.bLoadData);

bsave = (Button)findViewById(R.id.bSaveData);

etData = (EditText)findViewById(R.id.etData);

tvData = (TextView)findViewById(R.id.tvData);

bload.setOnClickListener(this);

bsave.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.bSaveData:

String stringData = etData.getText().toString();

SharedPreferences.Editor editor = sp.edit();

editor.putString("ourStringData", stringData);

editor.commit();

break;

case R.id.bLoadData:

sp = getSharedPreferences(filename, 0);

String getStringData = sp.getString("ourStringData", "Data Not Available");

tvData.setText(getStringData);

break;

}

}

 

}

 

  • We first initialise the variables, then we make a references of SharePreferences.

  • We then make an object of it using the mthod getSharedPreferences().

  • In this method we pass the filename where we want our preferences to be stored.

  • Within the onClick() method of the buttons we define the functions performed.

  • When the Save button is clicked then the string in the EditText is stored in the file.

  • We use the Editor to edit the contents of the file and the commit() method incorporates the changes in the file.

  • When the Load button is clicked, that time the string is retrieved and displayed in the TextView.

  • If there is no string as such, i.e the user presses the Load button before saving the string, the defualt string in the getString() method, i.e, in this case “Data Not Available” will be displayed.

  • Below are the Screenshots of the application.

 

 

Pr.Pg border                                              Next Pg