Wednesday, September 22, 2021

Login and Registration in Firebase Android Studio

 



Login XML Code:- 

<?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=".Login">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:id="@+id/signemal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/signpass"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/loginbtn"/>
<TextView
android:id="@+id/gotoregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_gravity="center"
android:textSize="25sp"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/loginprogress"/>
</LinearLayout>

Register Xml Code:-

<?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=".Register">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"
android:id="@+id/registername"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:id="@+id/registemai"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:id="@+id/registerpass"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Signup"
android:id="@+id/signupbtn"/>
<TextView
android:id="@+id/gotosiggin"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signin"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/registerprogress"/>
</LinearLayout>

Login Java Code :-

package com.niranjan.ecomerceapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class Login extends AppCompatActivity {
EditText logemail,logpass;
Button logbtn;
TextView gotoreg;
FirebaseAuth auth;
ProgressBar loginprogress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth =FirebaseAuth.getInstance();
loginprogress=findViewById(R.id.loginprogress);
loginprogress.setVisibility(View.GONE);
logemail=findViewById(R.id.signemal);
logpass=findViewById(R.id.signpass);
logbtn=findViewById(R.id.loginbtn);
gotoreg=findViewById(R.id.gotoregister);
gotoreg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Register.class));
}
});
logbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

loginuser();
loginprogress.setVisibility(View.VISIBLE);
}
});
}

private void loginuser() {

String userEmail = logemail.getText().toString();
String userPassword = logpass.getText().toString();

if (TextUtils.isEmpty(userEmail)){
Toast.makeText(this, "Email is Empty", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(userPassword)){
Toast.makeText(this, "Password is Empty", Toast.LENGTH_SHORT).show();
return;
}
if (userPassword.length()<6){
Toast.makeText(this, "Password length must be gratter than 6 letter", Toast.LENGTH_SHORT).show();
return;
}
auth.signInWithEmailAndPassword(userEmail,userPassword)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){

Toast.makeText(Login.this, "Login Sucessfully", Toast.LENGTH_SHORT).show();

}else {
Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
}
}



Register Java Code:-

package com.niranjan.ecomerceapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;

public class Register extends AppCompatActivity {
EditText regname,regemail,regpass;
Button regbtn;
TextView gotosign;
FirebaseAuth auth;
FirebaseDatabase database;
ProgressBar regprogress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
auth=FirebaseAuth.getInstance();
regprogress=findViewById(R.id.registerprogress);
regprogress.setVisibility(View.GONE);
database=FirebaseDatabase.getInstance();
regname=findViewById(R.id.registername);
regemail=findViewById(R.id.registemai);
regpass=findViewById(R.id.registerpass);
regbtn=findViewById(R.id.signupbtn);
gotosign=findViewById(R.id.gotosiggin);
gotosign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Register.this,Login.class));
}
});
regbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createUser();
regprogress.setVisibility(View.VISIBLE);
}
});

}
private void createUser(){
String userName = regname.getText().toString();
String userEmail = regemail.getText().toString();
String userPassword = regpass.getText().toString();
if (TextUtils.isEmpty(userName)){
Toast.makeText(this, "Name is Empty", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(userEmail)){
Toast.makeText(this, "Email is Empty", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(userPassword)){
Toast.makeText(this, "Password is Empty", Toast.LENGTH_SHORT).show();
return;
}
if (userPassword.length()<6){
Toast.makeText(this, "Password length must be gratter than 6 letter", Toast.LENGTH_SHORT).show();
return;
}
auth.createUserWithEmailAndPassword(userEmail,userPassword)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Usermodel usermodel=new Usermodel(userName,userEmail,userPassword);
String id = task.getResult().getUser().getUid();
database.getReference().child("Admin").child(id).setValue(usermodel);
regprogress.setVisibility(View.GONE);
Toast.makeText(Register.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Register.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
}
}


Only Java Code Usermodel :-


package com.niranjan.ecomerceapp;

public class Usermodel {
String name,email,password;

public Usermodel() {
}

public Usermodel(String name, String email, String password) {
this.name = name;
this.email = email;
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}


Youtube Channel:- sirfandroid





No comments:

Post a Comment

Activity Navigation One Activity to another Activity

Main Activity :-   <? xml version ="1.0" encoding ="utf-8" ?> < RelativeLayout xmlns: android ="http://sch...