Firebase Authentication | User Login using Firebase in Android

 Step 1 Login Activity.xml(Launcher)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/gradient"
    android:gravity="center"
    android:padding="16dp"
    tools:context=".Login">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <androidx.cardview.widget.CardView xmlns:Card_View="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:layout_margin="5dp"
            android:padding="10dp"
            Card_View:cardCornerRadius="5dp"
            Card_View:cardElevation="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:orientation="vertical">

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/emailError"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp">

                    <EditText
                        android:id="@+id/email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:hint="@string/prompt_email"
                        android:inputType="textEmailAddress"
                        android:maxLines="1"
                        android:paddingStart="5dp"
                        android:singleLine="true" />

                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/passError"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    app:passwordToggleEnabled="true">

                    <EditText
                        android:id="@+id/password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:hint="@string/prompt_password"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:paddingStart="5dp"
                        android:singleLine="true" />

                </com.google.android.material.textfield.TextInputLayout>

                <Button
                    android:id="@+id/login"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="20dp"
                    android:background="@drawable/button_gradient"
                    android:text="@string/login"
                    android:textColor="@android:color/white"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="@string/create_new"
                    android:textColor="@color/colorPrimary"
                    android:textSize="16sp" />

            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </RelativeLayout>

    <ImageView
        android:id="@+id/profile"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-50dp"
        android:background="@drawable/round_background"
        android:padding="15dp"
        android:src="@drawable/profile" />


</RelativeLayout>

 Step 2 Login Activity.Java

package com.example.loginapp;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class Login extends AppCompatActivity {

EditText email1, password1;
Button login;
TextView register;
boolean isEmailValid, isPasswordValid;
TextInputLayout emailError, passError;

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mAuth = FirebaseAuth.getInstance();

email1 = (EditText) findViewById(R.id.email);
password1 = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
register = (TextView) findViewById(R.id.register);
emailError = (TextInputLayout) findViewById(R.id.emailError);
passError = (TextInputLayout) findViewById(R.id.passError);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SetValidation();
}
});

register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// redirect to RegisterActivity
Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(intent);
}
});
}

public void SetValidation() {

String email, password, name;
email = email1.getText().toString();
password = password1.getText().toString();




// Check for a valid email address.
if (email1.getText().toString().isEmpty()) {
emailError.setError(getResources().getString(R.string.email_error));
isEmailValid = false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(email1.getText().toString()).matches()) {
emailError.setError(getResources().getString(R.string.error_invalid_email));
isEmailValid = false;
} else {
isEmailValid = true;
emailError.setErrorEnabled(false);
}

// Check for a valid password.
if (password1.getText().toString().isEmpty()) {
passError.setError(getResources().getString(R.string.password_error));
isPasswordValid = false;
} else if (password1.getText().length() < 6) {
passError.setError(getResources().getString(R.string.error_invalid_password));
isPasswordValid = false;
} else {
isPasswordValid = true;
passError.setErrorEnabled(false);
}

if (isEmailValid && isPasswordValid) {
Toast.makeText(getApplicationContext(), "Successfully", Toast.LENGTH_SHORT).show();

mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(
@NonNull Task<AuthResult> task)
{
if (task.isSuccessful()) {
// Toast.makeText(getApplicationContext(),
// "Login successful!!",
// Toast.LENGTH_LONG)
// .show();

// hide the progress bar
// progressbar.setVisibility(View.GONE);

// if sign-in is successful
// intent to home activity
Intent intent
= new Intent(Login.this,
MainActivity.class);
startActivity(intent);
finish();
}

else {

// sign-in failed
Toast.makeText(getApplicationContext(),
"Login failed!!",
Toast.LENGTH_LONG)
.show();

// hide the progress bar
// progressbar.setVisibility(View.GONE);
}
}
});
}

}
}


 Step 3 Register Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".RegisterActivity"
android:padding="16dp"
android:background="@drawable/gradient"
android:scrollbarThumbVertical="@null">



<androidx.cardview.widget.CardView
xmlns:Card_View="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:padding="10dp"
Card_View:cardCornerRadius="5dp"
Card_View:cardElevation="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/nameError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/name"
android:inputType="textNoSuggestions"
android:maxLines="1"
android:paddingStart="5dp"
android:singleLine="true"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingStart="5dp"
android:singleLine="true"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phoneError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">

<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/phone_number"
android:maxLength="12"
android:inputType="number"
android:maxLines="1"
android:paddingStart="5dp"
android:singleLine="true"/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginTop="5dp">

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/prompt_password"
android:inputType="textPassword"
android:maxLines="1"
android:paddingStart="5dp"
android:singleLine="true"/>

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="@drawable/button_gradient"
android:text="@string/register"
android:textColor="@android:color/white"
android:textSize="16sp"/>

<TextView
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/not_register"
android:textColor="@color/colorPrimary"
android:textSize="16sp"/>

</LinearLayout>
</androidx.cardview.widget.CardView>


</ScrollView>

 Step 4 RegisterActivity.Java

package com.example.loginapp;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class RegisterActivity extends AppCompatActivity {

EditText name1, email1, phone1, password1;
Button register;
TextView login;
boolean isNameValid, isEmailValid, isPhoneValid, isPasswordValid;
TextInputLayout nameError, emailError, phoneError, passError;

private FirebaseAuth mAuth;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

mAuth = FirebaseAuth.getInstance();

name1 = (EditText) findViewById(R.id.name);
email1 = (EditText) findViewById(R.id.email);
phone1 = (EditText) findViewById(R.id.phone);
password1 = (EditText) findViewById(R.id.password);
login = (TextView) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
nameError = (TextInputLayout) findViewById(R.id.nameError);
emailError = (TextInputLayout) findViewById(R.id.emailError);
phoneError = (TextInputLayout) findViewById(R.id.phoneError);
passError = (TextInputLayout) findViewById(R.id.passError);

register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SetValidation();
}
});

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// redirect to LoginActivity
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
}
});
}

public void SetValidation() {

String email, password, name;
int phone;
email = email1.getText().toString();
password = password1.getText().toString();
name = name1.getText().toString();


// Check for a valid name.
if (name1.getText().toString().isEmpty()) {
nameError.setError(getResources().getString(R.string.name_error));
isNameValid = false;
} else {
isNameValid = true;
nameError.setErrorEnabled(false);
}

// Check for a valid email address.
if (email1.getText().toString().isEmpty()) {
emailError.setError(getResources().getString(R.string.email_error));
isEmailValid = false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(email1.getText().toString()).matches()) {
emailError.setError(getResources().getString(R.string.error_invalid_email));
isEmailValid = false;
} else {
isEmailValid = true;
emailError.setErrorEnabled(false);
}

// Check for a valid phone number.
if (phone1.getText().toString().isEmpty()) {
phoneError.setError(getResources().getString(R.string.phone_error));
isPhoneValid = false;
} else {
isPhoneValid = true;
phoneError.setErrorEnabled(false);
}

// Check for a valid password.
if (password1.getText().toString().isEmpty()) {
passError.setError(getResources().getString(R.string.password_error));
isPasswordValid = false;
} else if (password1.getText().length() < 6) {
passError.setError(getResources().getString(R.string.error_invalid_password));
isPasswordValid = false;
} else {
isPasswordValid = true;
passError.setErrorEnabled(false);
}

if (isNameValid && isEmailValid && isPhoneValid && isPasswordValid) {
Toast.makeText(getApplicationContext(), "Successfully", Toast.LENGTH_SHORT).show();


mAuth
.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Toast.makeText(getApplicationContext(),
// "Registration successful!",
// Toast.LENGTH_LONG)
// .show();

// hide the progress bar


// if the user created intent to login activity
Intent intent
= new Intent(RegisterActivity.this, Login.class);
startActivity(intent);
finish();
} else {

// Registration failed
Toast.makeText(
getApplicationContext(),
"Registration failed!!"
+ " Please try again later",
Toast.LENGTH_LONG)
.show();

// hide the progress bar

}
}
});


}


}
}



 Step 5 Add In Your String

<resources>
<string name="app_name">LoginApp</string>
<string name="prompt_email">Email</string>
<string name="prompt_password">Password</string>
<string name="login">Login</string>
<string name="create_new">Click here for register</string>
<string name="not_register">Not Register?</string>
<string name="name">Name</string>
<string name="phone_number">Phone Number</string>
<string name="register">Register</string>
<string name="name_error">Please enter your name</string>
<string name="phone_error">Please enter your phone number</string>
<string name="email_error">Please enter your email address</string>
<string name="error_invalid_email">This email address is invalid</string>
<string name="password_error">Please enter a password</string>
<string name="error_invalid_password">Please enter a minimum password of 6 characters</string>
</resources>

 Step 6 Dependencies 

implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-auth'