Native Mobile App Development With Java

Android sharedpreferences Example

15-Sep-2024

Learn how to use SharedPreferences to store and retrieve data in Android Studio project using Java


1/  Create A New Project  And set Button in XML



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preferences Example"
android:textSize="25dp"
android:layout_marginBottom="20dp"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/setBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Data"
tools:ignore="MissingConstraints" />

<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Data"
tools:ignore="MissingConstraints" />
</LinearLayout>

</RelativeLayout>





1/  Set Sharedpreferences in  MainActivity.java





package com.example.sharedpreferences;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;

SharedPreferences.Editor myEdit;

Button setBtn,getBtn;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
myEdit = sharedPreferences.edit();

setBtn=findViewById(R.id.setBtn);
getBtn=findViewById(R.id.getBtn);

setBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myEdit.putString("product", "Banana");
myEdit.putInt("price", 10);
myEdit.commit();
Toast.makeText(MainActivity.this, "Shared Preferences Set", Toast.LENGTH_SHORT).show();
}
});


getBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = sharedPreferences.getString("product", "");
int age = sharedPreferences.getInt("price", 0);


Toast.makeText(MainActivity.this, name+"", Toast.LENGTH_SHORT).show();
}
});

}
}


Run code then see output.

Comments