skip to Main Content

Well, I get the user inside the Firebase database but it doesn’t seem to be opening up the login activity after clicking register. It endlessly shows the progress bar…

Heres the entire code block

package com.example.goalhero;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {

    EditText etRegFullname, etRegEmail, etRegDob, etRegMobile, etRegPwd, etRegConfirmPwd;
    ProgressBar progressBar;
    RadioGroup rgRegGender;
    RadioButton rbRegGenderSelected;
    DatePickerDialog picker;

    private static final String TAG = "RegisterActivity";

    protected void onCreate(Bundle savedInstanceState) {


        Toast.makeText(RegisterActivity.this, "You may register now.", Toast.LENGTH_LONG).show();

        progressBar = findViewById(;

        etRegPwd = findViewById(;
        etRegConfirmPwd = findViewById(;

        etRegFullname = findViewById(;
        etRegEmail = findViewById(;
        etRegDob = findViewById(;
        etRegMobile = findViewById(;

        rgRegGender = findViewById(;

        // Setting up the date picker
        etRegDob.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final Calendar cal = Calendar.getInstance();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int month = cal.get(Calendar.MONTH);
                int year = cal.get(Calendar.YEAR);

                // Date picker dialog
                picker = new DatePickerDialog(RegisterActivity.this, new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        etRegDob.setText(day + "/" + (month + 1) + "/" + year);
                        Log.d(TAG, "Date of Birth selected: " + etRegDob.getText().toString());
                }, year, month, day);

        Button registerBtn = findViewById(;

        registerBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                int selectedGenderId = rgRegGender.getCheckedRadioButtonId();
                rbRegGenderSelected = findViewById(selectedGenderId);

                // Obtain the entered data
                String txtFullname = etRegFullname.getText().toString();
                String txtEmail = etRegEmail.getText().toString();
                String txtDob = etRegDob.getText().toString();
                String txtMobile = etRegMobile.getText().toString();
                String txtPwd = etRegPwd.getText().toString();
                String txtConfirmPwd = etRegConfirmPwd.getText().toString();
                String txtGender;

                Log.d(TAG, "Register button clicked with details - Fullname: " + txtFullname + ", Email: " + txtEmail + ", DOB: " + txtDob + ", Mobile: " + txtMobile + ", Password: " + txtPwd + ", Confirm Password: " + txtConfirmPwd);

                // Validate the mobile number using matcher and pattern (regular express)
                String mobileRegex = "^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$";
                Matcher mobileMatcher;
                Pattern mobilePattern = Pattern.compile(mobileRegex);
                mobileMatcher = mobilePattern.matcher(txtMobile);

                if (TextUtils.isEmpty(txtFullname)) {
                    showError(etRegFullname, "Please enter your full name.", "Full name is required.");
                } else if (TextUtils.isEmpty(txtEmail)) {
                    showError(etRegEmail, "Please enter a valid email.", "Email is required.");
                } else if (!Patterns.EMAIL_ADDRESS.matcher(txtEmail).matches()) {
                    showError(etRegEmail, "Please re-enter your email.", "Valid email address required.");
                } else if (TextUtils.isEmpty(txtDob)) {
                    showError(etRegDob, "Please enter your DOB.", "DOB required.");
                } else if (rgRegGender.getCheckedRadioButtonId() == -1) {
                    showError(rbRegGenderSelected, "Please select your gender.", "Gender selection required.");
                } else if (TextUtils.isEmpty(txtMobile)) {
                    showError(etRegMobile, "Please enter your 10-digit mobile number.", "Phone number isn't valid.");
                } else if (txtMobile.length() != 10) {
                    showError(etRegMobile, "Phone number must be 10-digits", "Phone number isn't 10-digits.");
                } else if (!mobileMatcher.find()) {
                    showError(etRegMobile, "This is not a valid phone number.", null);
                } else if (TextUtils.isEmpty(txtPwd)) {
                    showError(etRegPwd, "You must enter a password.", "No password entered.");
                } else if (txtPwd.length() < 6) {
                    showError(etRegPwd, "Password too short.", "Password too short.");
                } else if (TextUtils.isEmpty(txtConfirmPwd)) {
                    showError(etRegConfirmPwd, "Please confirm your password.", "Password confirmation is required.");
                } else if (!txtPwd.equals(txtConfirmPwd)) {
                    showError(etRegConfirmPwd, "Passwords must match.", "Passwords don't match.");
                } else {
                    txtGender = rbRegGenderSelected.getText().toString();
                    registerUser(txtFullname, txtEmail, txtDob, txtGender, txtMobile, txtPwd);

    private void showError(View view, String toastMessage, String errorMessage) {
        Toast.makeText(RegisterActivity.this, toastMessage, Toast.LENGTH_SHORT).show();
        Log.e(TAG, "Error: " + toastMessage + " - " + errorMessage);
        if (view instanceof EditText) {
            ((EditText) view).setError(errorMessage);
        } else if (view instanceof RadioButton) {
            ((RadioButton) view).setError(errorMessage);

    private void registerUser(String txtFullname, String txtEmail, String txtDob, String txtGender, String txtMobile, String txtPwd) {
        FirebaseAuth auth = FirebaseAuth.getInstance();

        auth.createUserWithEmailAndPassword(txtEmail, txtPwd)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser firebaseUser = task.getResult().getUser();
                            Log.d(TAG, "User registered successfully with UID: " + firebaseUser.getUid());
                            updateUserProfile(firebaseUser, txtFullname);
                            writeUserDataToDatabase(firebaseUser, new ReadWriteUserDetails(txtDob, txtGender, txtMobile));
                        } else {

    private void updateUserProfile(FirebaseUser user, String displayName) {
        user.updateProfile(new UserProfileChangeRequest.Builder()
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "User profile updated.");
                        } else {
                            Log.e(TAG, "Failed to update user profile.");
                            Toast.makeText(RegisterActivity.this, "Failed to update user profile.", Toast.LENGTH_SHORT).show();

    private void writeUserDataToDatabase(FirebaseUser user, ReadWriteUserDetails userData) {
        DatabaseReference referenceProfile = FirebaseDatabase.getInstance().getReference("Registered Users");
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "User data written to database.");
                        } else {
                            Log.e(TAG, "Failed to save user data.");
                            Toast.makeText(RegisterActivity.this, "Failed to save user data.", Toast.LENGTH_SHORT).show();

    private void sendVerificationEmail(FirebaseUser user) {
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "Verification email sent.");
                            Toast.makeText(RegisterActivity.this, "User registered successfully. Please verify your email.", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        } else {
                            Log.e(TAG, "Failed to send verification email.");
                            Toast.makeText(RegisterActivity.this, "Failed to send verification email.", Toast.LENGTH_SHORT).show();

    private void handleRegistrationError(Exception e) {
        String message;
        if (e instanceof FirebaseAuthWeakPasswordException) {
            message = "Password must contain 6 or more characters. Letters, numbers, and special character required";
        } else if (e instanceof FirebaseAuthInvalidCredentialsException) {
            message = "Your email is invalid or already in use.";
        } else if (e instanceof FirebaseAuthUserCollisionException) {
            message = "User is already registered.";
        } else {
            message = e.getMessage();
        Log.e(TAG, "Registration error: " + message, e);
        Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();

This is the output of the code

RegisterActivity com.example.goalhero D Date of Birth selected: 6/6/2024
RegisterActivity com.example.goalhero D Register button clicked with details – Fullname: K, Email: [email protected], DOB: 6/6/2024, Mobile: 2042322553, Password: *******, Confirm Password: *******
RegisterActivity com.example.goalhero D User registered successfully with UID: T49FfhdctwOcevRDuA94K0IDw8t2
RegisterActivity com.example.goalhero D User profile updated.



  1. Chosen as BEST ANSWER

    I just moved this

    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

    Into the registerUser method... Got rid of the email verification all together.

  2. Firstly I would highly suggest separate each functionality to a separate function, for example you would do something like:

    • registerUser(String txtFullname, String txtEmail, String txtDob, String txtGender, String txtMobile, String txtPwd)
    • updateUserProfile(FirebaseUser user, String displayName)
    • writeUserDataToDatabase(FirebaseUser user, ReadWriteUserDetails userData)
    • sendVerificationEmail(FirebaseUser user)
    • handleRegistrationError(Exception e)

    This will improve code organization, reusability and maintainability.

    Also I would suggest moving sending the verification email after successful user registration to ensure the user object is valid.

    You can see a complete example reflecting my points in Here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top