Click here to Skip to main content
13,259,539 members (64,137 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 20 Jan 2014

Shared Preferences (ANDROID)

, 20 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Using shared preferences in Android


Interface for accessing and modifying preference data is returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage.


Sometimes, you need to keep store the record without saving in sqlite or using any storage element. Shared Preference is used for storing the values even after you close the app.

Using the Code

We are going to make a project that will store the text what you have typed in the editText box and when you close the app and open it again, it will show the text in the editText box that you have written before.

  • Create a project.
  • Add a editText box to the XML file.
  • Open the Java file and type the code...

The code is as follows:

package com.prefe.project;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class SharedPreferencesActivity<et> extends Activity {

private EditText et;

    public void onCreate(Bundle savedInstanceState) {
        et = (EditText) findViewById(;
        SharedPreferences settings = getSharedPreferences("MYPREFS",0);
        et.setText(settings.getString("tvalue", ""));       
protected void onStop() {
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("tvalue", et.getText().toString());




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ahmed Jubayer
Software Developer CodeMagnets Ltd.
Bangladesh Bangladesh
Hi, i'm Jubayer Ahmed.I started my programming career with c and c++.Later got a chance to develop windows/web applications using Java and ASP.Net.I Also developed some mobile application for android and iPhone. My interests involves programming, reading articles, know new technologies etc.

You may also be interested in...


Comments and Discussions

QuestionNot an article Pin
OriginalGriff25-Jan-12 0:42
mvpOriginalGriff25-Jan-12 0:42 
QuestionBit light Pin
DaveAuld24-Jan-12 23:17
mentorDaveAuld24-Jan-12 23:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 20 Jan 2014
Article Copyright 2014 by Ahmed Jubayer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid