Shared Preferences

Shared preference in Android tutorial

Shared preference is a way of storing and retrieving a small amount of data in the form of key/value pairs in a file on a storage device, such as strings, integers, floating-point numbers, and Boolean values, which are formed in the XML file of the application their preferences. In the device memory.

In the DATA/data/application package directory, Android saves Shared Preferences settings as an XML file in the shared prefs folder. Environment.getDataDirectory() can be used to get the DATA folder.

Important modes in shared preferences

ModeAbout
MODE_PRIVATE  It will keep files confidential and protect user data.
MODE_PUBLICIt will make the file publicly available to other applications on the device.
MODE_APPEND It is used when reading data from SP files.
methodAbout
apply()It’s an abstract method. It transfers your changes from the editor to the shared preference object you have called.
commit()commit () writes the data synchronously (blocks the thread from which it is called) and then informs you of the success of the operation.

Preview of Shared preference

File: MainActivity.kt

–> In MainActivity.kt file initialize variable and implement SharedPreferences method.

package com.sagar.sharedprefrence

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


class MainActivity : AppCompatActivity() {

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


        val button = findViewById<Button>(R.id.button)
        val textView = findViewById<TextView>(R.id.textView)
        val editText = findViewById<EditText>(R.id.editText)
        button.setOnClickListener{
            val msg = editText.text.toString()
            val shared = getSharedPreferences("Sample", MODE_PRIVATE)
            val editor = shared.edit()
            editor.putString("MSG", msg)
            editor.apply()
            textView.text = msg
        }

        // Getting  value  back from SharedPreferences
        val getShared = getSharedPreferences("Sample", MODE_PRIVATE)
        val value = getShared.getString("MSG", "Your data is show here")
        textView.text = value
    }
}

File: activity_main.xml

–> activity_main.xml contain button and edittext,textview.

<?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_marginTop="64dp"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/editText"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.114" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="68dp"
        android:text="Save "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="520dp"
        android:ems="10"
        android:hint="Enter here"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • We hope that this guide will assist you in quickly about Fragments. If you have any problems, please post them in the comments section and we will gladly assist you.

http://theudaipurstore.com

ViewPager using Fragment in Android with Example

Android RecyclerView in Kotlin with Example

Transform in CSS | Web Development

This Post Has One Comment

Leave a Reply