Create Firebase Register Activity With Email Verification In Android Studio – JAVA

RegisterActivity.java

package com.socialcodia.socialcodia;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
public class RegisterActivity extends AppCompatActivity {
    private EditText inputEmail, inputPassword, inputConfirmPassword;
    private TextView tvLogin;
    private Button btnRegister;
    FirebaseAuth mAuth;
    String email, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //Init
        inputEmail = findViewById(R.id.inputEmail);
        inputPassword = findViewById(R.id.inputPassword);
        inputConfirmPassword = findViewById(R.id.inputConfirmPassword);
        tvLogin = findViewById(R.id.tvLogin);
        btnRegister = findViewById(R.id.btnRegister);
        //Firebase Init
        mAuth = FirebaseAuth.getInstance();
        //On click listener to tv login to move to login activity
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendToLogin();
            }
        });
        //on click listener at btn register
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValidateDataAndDoRegister();
            }
        });
    }
    private void sendToLogin()
    {
        Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
        startActivity(intent);
    }
    private void ValidateDataAndDoRegister()
    {
        email = inputEmail.getText().toString().trim();
        password = inputPassword.getText().toString().trim();
        String confirmPassword = inputConfirmPassword.getText().toString().trim();
        if (email.isEmpty())
        {
            inputEmail.setError("Enter Email Address");
            inputEmail.requestFocus();
        }
        else if (email.length()<10)
        {
            inputEmail.setError("Enter valid email");
            inputEmail.requestFocus();
        }
        else if (password.isEmpty())
        {
            inputPassword.setError("Enter Password");
            inputPassword.requestFocus();
        }
        else if (inputPassword.length()<7)
        {
            inputPassword.setError("Password should be greater than 7 character");
            inputPassword.requestFocus();
        }
        else if (confirmPassword.isEmpty())
        {
            inputConfirmPassword.setError("Enter Confirm Password");
            inputConfirmPassword.requestFocus();
        }
        else if (confirmPassword.length()<7)
        {
            inputConfirmPassword.setError("Password should be greater than 7 character");
            inputConfirmPassword.requestFocus();
        }
        else if (!password.equals(confirmPassword)) //if password not equal to confirm password
        {
            inputPassword.setError("Password not matched");
            inputPassword.requestFocus();
            inputConfirmPassword.setError("Password not matched");
            inputConfirmPassword.requestFocus();
            inputPassword.setText("");
            inputConfirmPassword.setText("");
        }
        else
        {
            doRegister(email,password); 
        }
    }
    private void doRegister(String email, String password)
    {
        btnRegister.setEnabled(false);
        mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful())
                {
                    sendVerificationEmail();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof FirebaseAuthUserCollisionException) //this exception means email already registered
                {
                    btnRegister.setEnabled(true);
                    inputEmail.setError("Email Already Registered");
                    inputEmail.requestFocus();
                }
                else
                {
                    btnRegister.setEnabled(true);
                    Toast.makeText(RegisterActivity.this, "Opps! Something went wrong", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void sendVerificationEmail()
    {
        if (mAuth.getCurrentUser()!=null)
        {
            mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful())
                    {
                        btnRegister.setEnabled(true);
                        sendToLoginWithEmailAndPassword();
                    }
                    else
                    {
                        btnRegister.setEnabled(true);
                        Toast.makeText(getApplicationContext(),"Oops! failed to send verification email",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
    private void sendToLoginWithEmailAndPassword()
    {
        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        intent.putExtra("email",email);
        intent.putExtra("password",password);
        //forgot to start activity
        startActivity(intent);
    }
}

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
    <ImageView
        android:id="@+id/registerImage"
        android:layout_width="250dp"
        android:layout_height="250dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/login_screen_image" />
    <EditText
        android:id="@+id/inputEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Email Address"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintTop_toBottomOf="@+id/registerImage" />
    <EditText
        android:id="@+id/inputPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/inputEmail" />
    <EditText
        android:id="@+id/inputConfirmPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:hint="Enter Confirm Password"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@+id/inputPassword" />
    <Button
        android:id="@+id/btnRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:text="Register"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:background="@drawable/btn_corner"
        app:layout_constraintTop_toBottomOf="@+id/inputConfirmPassword" />
    <TextView
        android:id="@+id/tvLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Already have an account? Login"
        android:layout_marginTop="20dp"
        android:layout_marginRight="16dp"
        android:textColor="#040666"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnRegister" />
</androidx.constraintlayout.widget.ConstraintLayout>