In this example, we will store string, int & boolean data into sharedpreferences on save button click. On view button click we will display sharedpreferences stored data into textview & Clear sharedpreferences stored data on clear button click. See the following example:
activity_main.xml
Add the following code in the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="15dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SharedPreference"
android:textSize="30dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"/>
<EditText
android:id="@+id/edt_UserId"
android:hint="Enter User Id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<EditText
android:id="@+id/edt_Name"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<EditText
android:id="@+id/edt_Email"
android:hint="Enter Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_Save"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Save"
android:textAllCaps="false"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_View"
android:layout_margin="5dp"
android:layout_weight="1"
android:textAllCaps="false"
android:text="View"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_Clear"
android:layout_margin="5dp"
android:layout_weight="1"
android:textAllCaps="false"
android:text="Clear"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/txt_Display_Info"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
</LinearLayout>Create new SharedPreference.kt class and add following code:
SharedPreference.kt
package com.study.kotlinkatta
import android.content.Context
import android.content.SharedPreferences
class SharedPreference (val context: Context) {
private val preference_Name = "Kotlinkatta"
val shared_Pref: SharedPreferences = context.getSharedPreferences(preference_Name, Context.MODE_PRIVATE)
/*Stored String Data*/
fun save_String(key_name: String, text: String) {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.putString(key_name, text)
editor.commit()
}
fun getPreferenceString(key_name: String): String? {
return shared_Pref.getString(key_name, null)
}
/*Stored Int Data*/
fun save_Int(key_name: String, value: Int) {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.putInt(key_name, value)
editor.commit()
}
fun getPreferenceInt(key_name: String): Int {
return shared_Pref.getInt(key_name, 0)
}
/*Stored Boolean Data*/
fun save_Boolean(key_name: String, status: Boolean) {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.putBoolean(key_name, status!!)
editor.commit()
}
fun getPreferenceBoolean(key_name: String, defaultValue: Boolean): Boolean {
return shared_Pref.getBoolean(key_name, defaultValue)
}
fun clearSharedPreference() {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.clear()
editor.commit()
}
fun removeValue(key_name: String) {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.remove(key_name)
editor.commit()
}
}
MainActivity.kt
Add the following code in the MainActivity.kt class.
package com.study.kotlinkatta
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() {
var sharedPreference:SharedPreference? = null
lateinit var edt_UserId: EditText
lateinit var edt_Name: EditText
lateinit var edt_Email: EditText
lateinit var btn_Save: Button
lateinit var btn_View: Button
lateinit var btn_Clear: Button
lateinit var txt_Display_Info: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sharedPreference =SharedPreference(this)
edt_UserId = findViewById(R.id.edt_UserId)
edt_Name = findViewById(R.id.edt_Name)
edt_Email = findViewById(R.id.edt_Email)
btn_Save = findViewById(R.id.btn_Save)
btn_View = findViewById(R.id.btn_View)
btn_Clear = findViewById(R.id.btn_Clear)
txt_Display_Info = findViewById(R.id.txt_Display_Info)
btn_Save.setOnClickListener {
val str_user_id = edt_UserId.text.toString()
val str_name = edt_Name.text.toString()
val str_email = edt_Email.text.toString()
if(str_user_id.equals("") || str_name.equals("") || str_email.equals("")){
Toast.makeText(this,"Please Enter Details.",Toast.LENGTH_SHORT).show()
}else {
sharedPreference!!.save_String("user_id",str_user_id)
sharedPreference!!.save_String("name",str_name)
sharedPreference!!.save_String("email",str_email)
Toast.makeText(this,"Data Saved Successfully.",Toast.LENGTH_SHORT).show()
txt_Display_Info.setText("")
}
}
btn_View.setOnClickListener {
val user_id = sharedPreference!!.getPreferenceString("user_id")
val name = sharedPreference!!.getPreferenceString("name")
val email = sharedPreference!!.getPreferenceString("email")
if(user_id == null || name == null || email == null){
Toast.makeText(this,"No Data Available.",Toast.LENGTH_SHORT).show()
txt_Display_Info.setText("")
}else {
txt_Display_Info.setText("Info:-" + "\nUser Id:" + user_id + "\nName: " + name + "\nEmail: " + email)
}
}
btn_Clear.setOnClickListener {
sharedPreference!!.clearSharedPreference()
Toast.makeText(this,"Data Clear Successfully.",Toast.LENGTH_SHORT).show()
txt_Display_Info.setText("")
}
}}Output:
Comments
Post a Comment