Shared Preference in Android application

Shared Preference in Android app


Today, I’m talking about SharedPreferences. In this tutorial I’ll talking about SharedPreferences and how to use it in application.

Shared Preference in Android application
Shared Preference in Android application

Video tutorial:

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 the method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

[java]
SharedPreferences getSharedPreferences(String name, int mode);
[/java]
First parameter is the key and second parameter is the operating mode.
You can use the following modes:

  • MODE_PRIVATE
  • MODE_WORLD_READABLE (deprecated in API level 17)
  • MODE_WORLD_WRITEABLE (deprecated in API level 17)

MODE_PRIVATE
File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

MODE_WORLD_READABLE
File creation mode: allow all other applications to have read access to the created file. Creating world-readable files is very dangerous, and likely to cause security holes in applications.

MODE_WORLD_WRITEABLE
File creation mode: allow all other applications to have write access to the created file. Creating world-readable files is very dangerous, and likely to cause security holes in applications.

You can save some data in the sharedpreferences by using SharedPreferences.Editor class.
[java]
private final static String PREF_NAME = “pref_name”;

Editor editor = sharedpreferences.edit();
editor.putString(PREF_NAME, “test value”);
editor.commit();
[/java]
You also can use data for key from string.xml file:
[java]
private final static String PREF_NAME = “pref_name”;

Editor editor = sharedpreferences.edit();
editor.putString(getString(R.id.pref_name), “test value”);
editor.commit();
[/java]
You can use following methods from Editor class:

  • apply()
    Commit your preferences changes back from this Editor to the SharedPreferences object.
  • clear()
    Mark in the editor to remove all values from the preferences.
  • commit()
    Commit your preferences changes back from this Editor to the SharedPreferences object.
  • putBoolean(String key, boolean value)
    Set a boolean value in the preferences editor.
  • putFloat(String key, float value)
    Set a float value in the preferences editor.
  • putInt(String key, int value)
    Set a integer value in the preferences editor.
  • putLong(String key, long value)
    Set a long value in the preferences editor.
  • putString(String key, String value)
    Set a String value in the preferences editor.
  • putStringSet(String key, Set values)
    Set a set of String values in the preferences editor.
  • remove(String key)
    Mark in the editor that a preference value should be removed.

SharedPreference let customise name of preference file and type of access (mode). When you are using DefaultSharedPreference you name of shared preference file is PACKAGE_NAME of your application and file with preference accessible just for your application.

We finished with theoretical part. Next step is create a test application. This application will use SharedPreferences. In this tutorial I will use TextInputLayout (you can read my tutorial about it).
First step is create a new project:

Create_project
Create_project

After it need to add dependency for using TextInputLayout in gradle file:
[java]
dependencies {
compile ‘com.android.support:design:23.1.1’
}
[/java]

After it need to add string constants to strings.xml file in res/ folder.
[xml]
Name
Age
Save (Shared Preferences)
Restore (Shared Preferences)
Save (Default Shared Preferences)
Restore (Default Shared Preferences)
[/xml]

Next step is create layout for MainActicity.
[xml]





CategoriesUncategorized

2 thoughts on “Shared Preference in Android application

  1. Hi Igor,

    The getSharedPreferences() method has more options for customization. You can use different MODEs and create many different files for storing preferences.

    /data/data/PACKAGE/shared_prefs/

Leave a Reply

Your email address will not be published. Required fields are marked *