Posts

SharedPreferences - Save and Retrieve Data || Android Studio || Kotlin Tutorial

In this post, we will learn how to save and retrieve data using SharedPreferences.

 In this post, we will learn how to save and retrieve data using SharedPreferences. We will save and then retrieve the username and password in our app. so let's begin with a few steps:





Step 1: Create a new project with an empty activity


Step 2: Add the following XML code in the 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">


<EditText
android:id="@+id/Edittext"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="200dp"
android:hint="username"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edittext2"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:hint="password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Edittext" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Edittext"
app:layout_constraintVertical_bias="0.334" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.826"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Edittext"
app:layout_constraintVertical_bias="0.334" />


</androidx.constraintlayout.widget.ConstraintLayout>



Step 3: Add the following kotlin code in MainActivity.kt file

package com.example.myapplication

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


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



val save = findViewById<Button>(R.id.button)
val load = findViewById<Button>(R.id.button2)
val editText = findViewById<EditText>(R.id.Edittext)
val editText2 = findViewById<EditText>(R.id.edittext2)
val sharedPref = getSharedPreferences("myPref", MODE_PRIVATE)





save.setOnClickListener {
val editor =sharedPref.edit()
val userName = editText.text.toString()
val pass = editText2.text.toString()
editor.apply {
putString("user_name",userName)
putString("passsword",pass)
apply()
}
}





load.setOnClickListener {
val userName = sharedPref.getString("user_name",null)
val pass = sharedPref.getString("passsword",null)
editText.setText(userName)
editText2.setText(pass)
}

}

}


Step 4: Now Run App.






keywords:

Kotlin, java, android apps programming, android development, android development for beginners, android studio, android studio tutorial, android tutorial, beginners, how to make android apps, tutorial, android studio shared preferences, shared preferences, sharedpreferences, android sharedpreferences, android shared preferences, store data, save data, android save data, android store data, android how to save data, android how to save variables, shared preferences tutoriallogin signup complete example with MVVM HILT, token implementation retrofit shared preference android, shared preference tutorial android hindi, save tokens using shared preference android, crud operations retrofit implementation tutorial, android for beginners tutorial hindi, android complete project with mvvm hilt coroutines, learn android programming hindi, android tutorial for beginners hindi, android development tutorial hindi







Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.