All about SharedPreferences in Android app- create, retrieve and delete the variable – Android Studio Tutorial

This video gives details and demonstrates to create, retrieve or delete the shared preference variables as your Android App data.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details:

package com.programmerworld.sharedpreferenceapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
}


public void buttonPut(View view){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.clear();
editor.putString("stringUserData", editText.getText().toString());
editor.putString("stringProgrammerWorld", "This is Programmer World");
editor.apply();
}

public void buttonGet(View view){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String stringSharedPref_UserData = sharedPreferences.getString("stringUserData", null);
String stringSharedPref_PWorld = sharedPreferences.getString("stringProgrammerWorld", null);

if (stringSharedPref_UserData == null &&
stringSharedPref_PWorld == null){
textView.setText("Shared Preference Variables do NOT exits");
}
else {
textView.setText(stringSharedPref_PWorld + "\n" + stringSharedPref_UserData);
}
}

public void buttonDeleteAll(View view){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
//removes all the shared preference variables
sharedPreferences.edit().clear().commit();
}

public void buttonDelete(View view){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
// removes specific shared preference variable
sharedPreferences.edit().remove("stringUserData").commit();
}
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="303dp"
android:layout_height="93dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.412"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="35dp"
android:onClick="buttonPut"
android:text="Put"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<EditText
android:id="@+id/editText"
android:layout_width="268dp"
android:layout_height="67dp"
android:layout_marginStart="47dp"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="SharedPreferences Text ..."
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginTop="36dp"
android:onClick="buttonGet"
android:text="Get"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:layout_marginTop="40dp"
android:onClick="buttonDeleteAll"
android:text="Delete All"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="78dp"
android:layout_marginTop="40dp"
android:onClick="buttonDelete"
android:text="Delete"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

No changes required in Manifest, gradle or any other files.

Demo of SharedPreferences variable data retireived and displayed in a textview

Leave a Reply