Fri. Nov 15th, 2024

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <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"
        tools:context=".Login"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Regitsration"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="email"
            android:id="@+id/email"
            android:textColorHint="@color/black"
            android:textColor="@color/design_default_color_error"
            android:textAllCaps="true"
            android:textSize="20sp"
            android:textStyle="italic"
            android:layout_margin="5dp"
            android:padding="5dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"
            android:id="@+id/password"
            android:textColorHint="@color/black"
            android:textColor="@color/design_default_color_error"
            android:textAllCaps="true"
            android:textSize="20sp"
            android:textStyle="italic"
            android:layout_margin="5dp"
            android:padding="5dp"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login"
            android:id="@+id/login"
            android:layout_margin="5dp"
            android:padding="5dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New User Register here"
            android:id="@+id/newuser"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:textSize="25sp"
            android:layout_margin="10dp"
            android:padding="10dp"/>

    </LinearLayout>
</layout>

MainActivity.kt

package com.androindian.sampleproject

import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.androindian.sampleproject.databinding.ActivityLoginBinding


class Login : AppCompatActivity() {

    var binding: ActivityLoginBinding? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login)


        

        binding?.login?.setOnClickListener {
            
                var s1 = binding?.email?.text.toString().trim()
                var s2 = binding?.password?.text.toString().trim()

     
                            //adding data to shared preferences
      var sharedPreferences = getSharedPreferences("Login", MODE_PRIVATE)
                            var editor = sharedPreferences.edit()
                            editor.putString("email", s1)
                            editor.putString("password", s2)
                            editor.commit()

                   var intent = Intent(this@Login, HomePage::class.java)
                            startActivity(intent)
                            finish()
                
            }


        }
    }

Homepage Activity.kt

package com.androindian.sampleproject

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.androindian.sampleproject.databinding.ActivityHomePageBinding

class HomePage : AppCompatActivity() {

    var binding:ActivityHomePageBinding?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=DataBindingUtil.setContentView(this,R.layout.activity_home_page)

        var sharedPreferences=getSharedPreferences("Login", MODE_PRIVATE)

        var s1=sharedPreferences.getString("email",null)
        var s2=sharedPreferences.getString("password",null)

        binding?.logindata?.text=s1+s2

        binding?.logout?.setOnClickListener {
            var editor=sharedPreferences.edit()
            editor.clear()
           // editor.remove()
            editor.apply();
            var intent= Intent(this@HomePage,Login::class.java)
            startActivity(intent)
        }

    }
}


activity_home_page.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<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"
    tools:context=".HomePage"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/logindata"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="logut"
        android:id="@+id/logout"/>

</LinearLayout>
</layout>

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *