Android SharedPreference Tutorial with Example – Read and write Data in SharedPreference

By | May 31, 2019
SharedPreference or Session Storage in Android Tutorial Example

What is Android SharedPreference ?

Android Provides Different Different way to store data in application.Like Internal Storage,External Storage,Sqlite Database and SharedPreference stored data.Shared Preference is a store application data from key and value pair.

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by call this method of Environment.getDataDirectory().

When you can uninstall application or clear data on the application then shared preference data will be lost.it is cleared data in the application.

SharedPreference is a two parameter given. first is Key and second is value key is a identify store data and then value is a you can stored data on put the value parameter.

SharedPreference create to call getSharedPreferences () method use.this method is return of sharedpreference instance.In this method you can give two parameter.first is shared preference file name and second is Mode. sharedpreference file is used to stored all data in the file and then Mode is a preference type of file.

SharedPreference Mode Type

MODE_APPEND : This method used to append the new preferences with the already existing preferences.

MODE_PRIVATE : This method used to the file can only be accessed using calling application.

MODE_ENABLE_WRITE_AHEAD_LOGGING : This method is used to Database open flag. When it is set , it would enable write ahead logging by default.

MODE_MULTI_PROCESS : This method is used to check for modification of preferences even if the sharedpreference instance has already been loaded.

MODE_WORLD_READABLE : This method is used to allow other application to read the preferences.

MODE_WORLD_WRITEABLE : This method is used to allow other application to write the preferences.

Syntax of SharedPreference

Shared Preference is save delete data is used to SharedPreference.Editor class.In this class you can used to save data delete data in sharedpreference.

SharedPreference is different different datatype of stored data in application and remove the data from SharedPreference.Now below the Method of used to stored,retrieve and delete data from the sharedPreference.

SharedPreference Methods

putString(String key, String value) : This method is used to save string value in sharedPreference.

putFloat(String key, float value) : This method is used to save Float value in sharedPreference.

putInt(String key, int value) : This method is used to save int value in sharedPreference.

remove(String key) : This method is used to remove the single data in sharedpreference. you can pass the key in parameter and removed this key of data in sharedpference.

clear() : This method is used to remove all data in shared preference.

apply/commit() : This method is used to you can put key and value and the editor is commit or apply to stored data from sharedpreference file. you can not apply or commit then data is not stored in sharedpreference file.

Syntax of SharedPreference Store Data.

SharedPreference Example Code

Create activity_main.xml file Following this code.

Create MainActivity.java File Following this code.

Above the example of used to create simple login form i have create two edittext to input username and password then click save button to create instance of sharedpreference and stored two string value is username and password to get edittext data.now in this shared preference is stored file to use editor.apply() method.when stored success in the shared preference then display message.

Now when stored data in sharedPreference to click display button is display the data on the textview. sharedPreference is retrive data to call getString() method.you can stored data of any data type and then retrive the same data type you can stored.