Shared Preferences in Android

Android provides its users with Shared Preferences. Shared Preferences is a method for storing and retrieving tiny quantities of primitive data as key/value pairs to a file on the device storage, such as String, int, float, and Boolean, which comprise your preferences in an XML file within the programme on the device storage of Android application.



Let's take one example of shared preferences


Step by step implementation

Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside activity_main.xml file. 

 <?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="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Shared Preferences"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:hint="Enter Name"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit2" />

<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_marginStart="20dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="20dp"
android:hint="Enter Number"
android:inputType="number"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit1" />

<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.211"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txgage" />

<TextView
android:id="@+id/txgage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.211"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSubmit" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 3: Create an MainActivity.kt file in your package folder .

 Implement the same invoke the following code inside MainActivity.kt file.

 import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.velodate.myapplication.databinding.ActivityMainActivityoneBinding

class MainActivityone : AppCompatActivity() {
private lateinit var mainBinding: ActivityMainActivityoneBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainActivityoneBinding.inflate(layoutInflater)
setContentView(mainBinding.root)

//Get name and age in pref data
val sh = getSharedPreferences("MySharedPref", MODE_PRIVATE)
val name = sh.getString("name", "")
val age = sh.getInt("age", 0)

// Setting the fetched data in the EditTexts
mainBinding.txtname.setText(name)
if (age != 0) {
mainBinding.txgage.setText(age.toString())
}
mainBinding.btnSubmit.setOnClickListener {
//save data in pref name and age
val sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE)
val myEdit = sharedPreferences.edit()
myEdit.putString("name", mainBinding.edit1.text.toString())
myEdit.putInt("age", mainBinding.edit2.text.toString().toInt())
myEdit.apply()
//Get name and age in pref data
val sh = getSharedPreferences("MySharedPref", MODE_PRIVATE)
val s1 = sh.getString("name", "")
val a = sh.getInt("age", 0)
// Setting the fetched data in the EditTexts
mainBinding.txtname.setText(s1)
mainBinding.txgage.setText(a.toString())

}
}
}

 

Step 4 : Output of above example


Happy coding!


365Bloggy June 10, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive