In this example, I will
show you how to maintain user login session.The user login session will be maintained in an app even if the app is closed. The user will always be in a logged in state
until he logouts. We will stored data into Shared Preferences.
Step.1
MainActivity.kt
Add the following code in the MainActivity.kt class.
package com.study.kotlinkatta
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.sign_up_window.view.*
class MainActivity : AppCompatActivity() {
var sharedPreference:SharedPreference? = null
lateinit var edt_email: EditText
lateinit var edt_password: EditText
lateinit var btn_login: Button
lateinit var btn_sign_up: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sharedPreference =SharedPreference(this)
edt_email = findViewById(R.id.edt_email)
edt_password = findViewById(R.id.edt_password)
btn_login = findViewById(R.id.btn_login)
btn_sign_up = findViewById(R.id.btn_sign_up)
val str_login_status = sharedPreference!!.getPreferenceString("login_status")
if (str_login_status!=null){
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)
finish()
}
btn_login.setOnClickListener {
val str_email_id = edt_email.text.toString()
val str_password = edt_password.text.toString()
if(str_email_id.equals("") || str_password.equals("")){
Toast.makeText(this,"Please Enter Details.",Toast.LENGTH_SHORT).show()
}else {
val email_id = sharedPreference!!.getPreferenceString("email_id")
val password = sharedPreference!!.getPreferenceString("password")
if(email_id.equals(str_email_id) && password.equals(str_password)){
sharedPreference!!.save_String("login_status","1")
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)
finish()
}else {
Toast.makeText(this,"User Not Available.",Toast.LENGTH_SHORT).show()
}
}
}
btn_sign_up.setOnClickListener {
fun_SignUp_PopupWindow()
}
}
private fun fun_SignUp_PopupWindow() {
val layoutInflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val popView: View = layoutInflater.inflate(R.layout.sign_up_window, null)
val popupWindow: PopupWindow
popupWindow = PopupWindow(popView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT,true)
popupWindow.showAtLocation(popView, Gravity.CENTER, 0, 0)
val btn_dia_submit = popView.findViewById(R.id.btn_dia_submit) as Button
btn_dia_submit.setOnClickListener {
val str_dia_email_id = popView.edt_dia_email_id.text.toString()
val str_dia_password = popView.edt_dia_password.text.toString()
if(str_dia_email_id.equals("") || str_dia_password.equals("")){
Toast.makeText(this,"Please Enter Details.",Toast.LENGTH_SHORT).show()
}else {
popupWindow.dismiss()
sharedPreference!!.save_String("email_id",str_dia_email_id)
sharedPreference!!.save_String("password",str_dia_password)
Toast.makeText(this,"Data Saved Successfully.",Toast.LENGTH_SHORT).show()
}
}
}
}
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="Login"
android:textSize="40dp"
android:fontFamily="sans-serif-black"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"/>
<EditText
android:id="@+id/edt_email"
android:hint="Enter Email 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_password"
android:hint="Enter Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<Button
android:layout_marginTop="10dp"
android:id="@+id/btn_login"
android:text="Login"
android:textAllCaps="false"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="10dp"
android:id="@+id/btn_sign_up"
android:textAllCaps="false"
android:text="Sign Up"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Step.3
sign_up_window.xml
Add the following code in
the sign_up_window.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9999"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_margin="10dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#757575">
<TextView
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textColor="#FFF"
android:textSize="25dp"
android:gravity="center"/>
<EditText
android:id="@+id/edt_dia_email_id"
android:hint="Enter Email 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_dia_password"
android:hint="Enter Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#E7E7E7"/>
<Button
android:id="@+id/btn_dia_submit"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Submit"
android:textAllCaps="false"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Step.4
SharedPreference.kt
Add the following code in
the SharedPreference.kt
class.
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)
}
fun clearSharedPreference() {
val editor: SharedPreferences.Editor = shared_Pref.edit()
editor.clear()
editor.commit()
}
}
Step.5
DashboardActivity.kt
Add the following code in
the DashboardActivity.kt class.
package com.study.kotlinkatta
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class DashboardActivity :
AppCompatActivity() {
var sharedPreference:SharedPreference? = null
lateinit var btn_log_out: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
sharedPreference =SharedPreference(this)
btn_log_out = findViewById(R.id.btn_log_out)
btn_log_out.setOnClickListener {
sharedPreference!!.clearSharedPreference()
Toast.makeText(this,"User
LogOut Successfully.",Toast.LENGTH_SHORT).show()
val intent = Intent(this,
MainActivity::class.java)
startActivity(intent)
finish()
}
}
}
Step.6
activity_dashboard.xml
Add the following code in
the activity_dashboard.xml class.
<?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"
tools:context=".DashboardActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:fontFamily="sans-serif-black"
android:textColor="#000000"
android:text="Welcome To DashBoard"
android:layout_marginTop="50dp"/>
<Button
android:layout_margin="30dp"
android:id="@+id/btn_log_out"
android:textAllCaps="false"
android:text="LogOut"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Comments
Post a Comment