Android Shared Preferences Tutorial PDF
Android Shared Preferences Tutorial PDF
Android - Home
Android - Overview
Android - Environment Setup
Android - Architecture
Android - Application Components
Android - Hello World Example
Android - Resources
Android - Activities
Android - Services
Android - Broadcast Receivers
Android - Content Providers
Android - Fragments
Android - Intents/Filters
Android - UI Layouts
Android - UI Controls
Android - Event Handling
Android - Styles and Themes
Android - Custom Components
Android - Discussion
Previous Page
Next Page
Android provides many ways of storing data of an application. One of this way is called Shared Preferences.
Shared Preferences allow you to save and retrieve data in the form of key,value pair.
In order to use shared preferences , you have to call a method getSharedPreferences() that returns a
SharedPreference instance pointing to the file that contains the values of preferences.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
The first parameter is the key and the second parameter is the MODE. Apart from private there are other modes
available that are listed below:
Sr.No
MODE_APPEND
This will append the new preferences with the already existing preferences
2
MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set , it would enable write ahead logging by default
3
MODE_MULTI_PROCESS
This method will check for modification of preferences even if the sharedpreference instance has
already been loaded
4
MODE_PRIVATE
By setting this mode , the file can only be accessed using calling application
5
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit
method of SharedPreference instance and will receive it in an editor object. Its syntax is
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
Apart from the putString method , there are methods available in the editor class that allows manipulation of
apply()
It is an abstract method. It will commit your changes back from editor to the sharedPreference object
you are calling
2
clear()
remove(String key)
It will remove the value whose key has been passed as a parameter
4
Example
This example demonstrates the use of the Shared Preferences. It display a screen with some text fields , whose
value are saved when the application is closed and brought back when it is opened again .
To experiment with this example , you need to run this on an actual device on after developing the application
according to the steps below:
Steps
Description
You will use Android studio to create an Android application under a package
com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and
Compile With at the latest version of Android SDK to use higher levels of APIs.
Modify src/MainActivity.java file to add progress code to display the spinning progress dialog.
Run the application and choose a running android device and install the application on it and verify the results.
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_marginTop="67dp"
android:hint="Name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Pass" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Email" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your
computer. To run the app from Android studio, open one of your project's activity files and click Run
icon from
the toolbar. Before starting your application, Android studio will display following window to select an option
where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen
Now just put in some text in the field. Like i put some random name and other information and click on save
button.
Now when you press save button , the text will be saved in the shared preferences. Now press back button and
exit the application. Now open it again and you will see all the text you have written back in your application.
Previous Page
Next Page
Advertisements
Write for us
FAQ's
Helping
Contact