Simplified Kotlin Shared Preference | No 3rd party

Android Shared Preferences Example Tutorial (CURD)

SharedPreference: to Store data in the form of value-key pairs with a simple Kotlin class.

var sp = SharedPreference(this);

In this tutorial, we’ll use Shared Preferences in our android application to store data in the form of key-value pair

Storing Data:

To store String, Int and Boolean data we have three methods with the same name and different parameters (Method overloading).
  • save("key-name1","string value")
  • save("key-name2",int value)
  • save("key-name3",boolean)
Retrieve Data:
To Retrieve the data stored in SharedPreferences use the following methods.
  • sp.getValueString("user_name")
  • sp.getValueInt("user_id")
  • sp.getValueBoolean("user_session",true)
Clear All Data:
To clear the entire SharedPreferences use the below code.
  • sp.clearSharedPreference()
Remove Specific Data: 
  • sp.removeValue("user_name")
Common Shared Preference Class
import android.content.Context
import android.content.SharedPreferences

class SharedPreference(private val context: Context) {
    private val PREFS_NAME = "coredata"
    private val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
    //********************************************************************************************** save all
    //To Store String data
    fun save(KEY_NAME: String, text: String) {

        val editor: SharedPreferences.Editor = sharedPref.edit()
        editor.putString(KEY_NAME, text)
        editor.apply()
    }
    //..............................................................................................
    //To Store Int data
    fun save(KEY_NAME: String, value: Int) {

        val editor: SharedPreferences.Editor = sharedPref.edit()
        editor.putInt(KEY_NAME, value)
        editor.apply()
    }
    //..............................................................................................
    //To Store Boolean data
    fun save(KEY_NAME: String, status: Boolean) {

        val editor: SharedPreferences.Editor = sharedPref.edit()
        editor.putBoolean(KEY_NAME, status)
        editor.apply()
    }
    //********************************************************************************************** retrieve selected
    //To Retrieve String
    fun getValueString(KEY_NAME: String): String? {

        return sharedPref.getString(KEY_NAME, "")
    }
    //..............................................................................................
    //To Retrieve Int
    fun getValueInt(KEY_NAME: String): Int {

        return sharedPref.getInt(KEY_NAME, 0)
    }
    //..............................................................................................
    // To Retrieve Boolean
    fun getValueBoolean(KEY_NAME: String, defaultValue: Boolean): Boolean {

        return sharedPref.getBoolean(KEY_NAME, defaultValue)
    }
    //********************************************************************************************** delete all
    // To clear all data
    fun clearSharedPreference() {

        val editor: SharedPreferences.Editor = sharedPref.edit()
        editor.clear()
        editor.apply()
    }
    //********************************************************************************************** delete selected
    // To remove a specific data
    fun removeValue(KEY_NAME: String) {
        val editor: SharedPreferences.Editor = sharedPref.edit()
        editor.remove(KEY_NAME)
        editor.apply()
    }
}
...

Comments