Open In App

How to Delete Shared Preferences Data in Android?

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, Shared Preferences is a method of storing and fetching primitive data with the help of a key. This is file is stored inside the application in the form of an XML file. It is used to save data of types int, long, string, boolean, etc. In this article, we will show you how you could save and delete data from Shared Preferences in Android. Follow the below steps once the IDE is ready.

Read more on Shared Preferences: Shared Preferences in Android with Example

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView and a Button as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:layout_marginTop="100sp"
        android:layout_centerHorizontal="true"/>
 
    <Button
        android:id="@+id/button_1"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="delete"/>
 
</RelativeLayout>


 

 

Step 3: Working with the MainActivity.kt file

 

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

 

Kotlin




package org.geeksforgeeks.deletesharedprefata
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Declaring and initializing
        // elements from layout file
        val mTextView1 = findViewById<TextView>(R.id.text_view_1)
        val mButton = findViewById<Button>(R.id.button_1)
 
        // Calling Shared Preferences and Editor
        val mSharedPreferences = getPreferences(Context.MODE_PRIVATE)
        val mEditor = mSharedPreferences.edit()
 
        // Using Editor to put string against a key value
        // KEY        VALUE
        // City        Delhi
        // Country    India
        mEditor.putString("City", "Delhi").putString("Country", "India").apply()
     
        // Displaying the Shared preferences
        // data in TextView when app launches
        mTextView1.text = "${mSharedPreferences.getString("City", "")}\n${mSharedPreferences.getString("Country", "")}"
 
        // When button is clicked, value against key "City"
        // is removed and updated Shared preferences
        // data is displayed in the TextView
        mButton.setOnClickListener {
            mEditor.remove("City").apply()
            Toast.makeText(applicationContext, "City removed", Toast.LENGTH_SHORT).show()
            mTextView1.text = "${mSharedPreferences.getString("City", "")}\n${mSharedPreferences.getString("Country", "")}"
        }
 
    }
}


 

 

Output:

 

You can see that when the app launches, both the values are displayed. When the button is clicked, the value of the key “City” is removed.

 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads