Tutorial

Android SharedPreferences using Kotlin

Published on August 3, 2022
author

Anupam Chugh

Android SharedPreferences using Kotlin

In this tutorial, we’ll learn how to implement SharedPreferences in our Android Application using Kotlin.

What is Android SharedPreferences?

SharedPreferences is part of the Android API since API level 1. It’s an interface that allows us to store/modify/delete data locally. Generally, it is used to cache user local data such as login forms. The data is stored in the form of a key-value pair. You can create multiple files to hold the SharedPreferences data.

SharedPreferences Methods

Let’s look at some important methods for SharedPreferences.

  • getSharedPreferences(String, int) method is used to retrieve an instance of the SharedPreferences. Here String is the name of the SharedPreferences file and int is the Context passed.
  • The SharedPreferences.Editor() is used to edit values in the SharedPreferences.
  • We can call commit() or apply() to save the values in the SharedPreferences file. The commit() saves the values immediately whereas apply() saves the values asynchronously.

SharedPreferences Setting/Retrieving Values using Kotlin

We can set values on our SharedPreference instance using Kotlin in the following way.

val sharedPreference =  getSharedPreferences("PREFERENCE_NAME",Context.MODE_PRIVATE)
var editor = sharedPreference.edit()
editor.putString("username","Anupam")
editor.putLong("l",100L)
editor.commit()

For retrieving a value:

sharedPreference.getString("username","defaultName")
sharedPreference.getLong("l",1L)

The permitted types on a SharedPreference instance are: android shared preference types

Kotlin Code to Clear and Remove SharedPreferences Records

We can also clear all the values or remove a particular value by calling clear() and remove(String key) methods.

editor.clear()
editor.remove("username")

Note: Changes made to the editor after the commit or apply aren’t considered. The above way to save and retrieve values from a SharedPreference is nearly the same as we do in Java. So where’s the magic of Kotlin? That’s what we’ll see next through an example android application.

Android SharedPreferences Kotlin Project Structure

android shared preference kotlin project In this application, we’ll have a login screen, which allows us to save/clear the form data.

1. Layout code

The code for the activity_main.xml layout file is given below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/inUserId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:hint="User ID"
        android:inputType="number" />

    <EditText
        android:id="@+id/inPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inUserId"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inPassword"
        android:text="SAVE USER DATA" />

    <Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/btnSave"
        android:text="CLEAR USER DATA" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inPassword"
        android:text="SHOW" />

    <Button
        android:id="@+id/btnShowDefault"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnSave"
        android:text="Show Default" />


</RelativeLayout>

2. MainActivity Kotlin Code

The code for the MainActivity.kt Kotlin class is given below.

package com.journaldev.androidlysharedpreferences

import android.content.Context
import android.content.SharedPreferences
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.View
import com.journaldev.androidlysharedpreferences.PreferenceHelper.defaultPreference
import com.journaldev.androidlysharedpreferences.PreferenceHelper.password
import com.journaldev.androidlysharedpreferences.PreferenceHelper.userId
import com.journaldev.androidlysharedpreferences.PreferenceHelper.clearValues
import com.journaldev.androidlysharedpreferences.PreferenceHelper.customPreference

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

    val CUSTOM_PREF_NAME = "User_data"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnSave.setOnClickListener(this)
        btnClear.setOnClickListener(this)
        btnShow.setOnClickListener(this)
        btnShowDefault.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        val prefs = customPreference(this, CUSTOM_PREF_NAME)
        when (v?.id) {
            R.id.btnSave -> {
                prefs.password = inPassword.text.toString()
                prefs.userId = inUserId.text.toString().toInt()
            }
            R.id.btnClear -> {
                prefs.clearValues

            }
            R.id.btnShow -> {
                inUserId.setText(prefs.userId.toString())
                inPassword.setText(prefs.password)
            }
            R.id.btnShowDefault -> {

                val defaultPrefs = defaultPreference(this)
                inUserId.setText(defaultPrefs.userId.toString())
                inPassword.setText(defaultPrefs.password)
            }
        }
    }


}

object PreferenceHelper {

    val USER_ID = "USER_ID"
    val USER_PASSWORD = "PASSWORD"

    fun defaultPreference(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

    fun customPreference(context: Context, name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)

    inline fun SharedPreferences.editMe(operation: (SharedPreferences.Editor) -> Unit) {
        val editMe = edit()
        operation(editMe)
        editMe.apply()
    }

    var SharedPreferences.userId
        get() = getInt(USER_ID, 0)
        set(value) {
            editMe {
                it.putInt(USER_ID, value)
            }
        }

    var SharedPreferences.password
        get() = getString(USER_PASSWORD, "")
        set(value) {
            editMe {
                it.putString(USER_PASSWORD, value)
            }
        }

    var SharedPreferences.clearValues
        get() = { }
        set(value) {
            editMe {
                it.clear()
            }
        }
}


Thanks to Kotlin Android Extensions, we don’t have to use findViewById for each XML view. In the above code, we are creating a singleton class using the object keyword. We are declaring an inline higher-order function named editMe(), which holds the logic for the edit operation. We’ve created separate properties for each of the values. We are using the get and set Kotlin properties to retrieve and set the data in the shared preferences. Kotlin has reduced the code verbosity and it looks much cleaner. Furthermore, we can make it more concise by using another Kotlin higher-order function shown below.

fun SharedPreferences.Editor.put(pair: Pair<String, Any>) {
    val key = pair.first
    val value = pair.second
    when(value) {
        is String -> putString(key, value)
        is Int -> putInt(key, value)
        is Boolean -> putBoolean(key, value)
        is Long -> putLong(key, value)
        is Float -> putFloat(key, value)
        else -> error("Only primitive types can be stored in SharedPreferences")
    }

And we do the following while setting the values:

var SharedPreferences.password
        get() = getString(USER_PASSWORD, "")
        set(value) {
            editMe {
                it.put(USER_PASSWORD to value)
            }
        }

This is as close as Kotlin can get you to the English Language. The output of the above application in action is given below. android shared preferences kotlin app output

You can download the source code from the following link: AndroidlySharedPreferences

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 19, 2021

Hello, I am interested in your implementation of a SharedPreference Util, but when I try to write it myself, I found that kotlin make the value in set(value) a ? type, so I have to make the put method from Pair to Pair, that seems not convenient at all, I guess kotlin update it’s grammar, so how can I implement this object PreferenceHelper now? Thanks.

- Daniel

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 14, 2020

    how to define preference inside fragment?

    - Ayush Pandey

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.