Android SharedPreferences using Kotlin

Pandiyan Mani
2 min readMar 26, 2023

--

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage

SharedPreferences Setting/Retrieving Values using Kotlin

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:

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")

Lets Jump in to the Coding Part where we store UserName and Display UserName.

So i am create an xml for getting UserName from User so inside activity_main.xml i added the EditText View along with that we have a TextView for Displaying saved UserName and Three Button one is for save, clear and Display Username..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/editusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter UserName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="Currently UserName is Empty"
>

</TextView>

<Button
android:id="@+id/save"
android:text="SAVE"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</Button>

<Button
android:id="@+id/clear"
android:text="CLEAR"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</Button>

<Button
android:id="@+id/display"
android:text="DISPLAY USERNAME"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</Button>

</LinearLayout>

And let see the complete code of MainActivity.kt

package com.student.sharedpreferencce

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

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

val sharedPreference = getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
var editor = sharedPreference.edit()

val save = findViewById<Button>(R.id.save)
val clear = findViewById<Button>(R.id.clear)
val display = findViewById<Button>(R.id.display)
val editusername = findViewById<EditText>(R.id.editusername)
val username = findViewById<TextView>(R.id.username)

username.text = sharedPreference.getString("username","")

save.setOnClickListener {
if(editusername.text.isNotEmpty()) {
editor.putString("username",editusername.text.toString())
editor.commit()
}
}

clear.setOnClickListener {
editor.clear()
editor.remove("ausername")
editor.apply()
}

display.setOnClickListener {
username.text = sharedPreference.getString("username","")
}
}
}

And above code are straight forward as we don't need any separate explanation Thanks Happy Coding.

Final Output:

--

--

No responses yet