javaandroidif-statementandroid-toast

Created a Registration Activity, Everything works fine except the last two inputs which are Password & Confirm password


Register Activity

The toast messages or errors of both password & confirm password doesn't show at all. Username, email is working fine. What could be the problem ?. First i tried to make different methods for username validation, email validation, password validation & confirm password but was having interrupted toasts then I just removed the methods and compiled them in one method but still having this last issue.

package com.example.foodeezfavourite;

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.Toast;

import com.google.android.material.textfield.TextInputEditText;
public class RegistrationActivity extends AppCompatActivity {

    TextInputEditText registrationUserName;
    EditText registrationEmail, registrationPassword, registrationConfirmPassword;
    Button registrationButton;
    String userName, email, password, confirmPassword, emailPattern, passwordPattern;

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

        registrationUserName = findViewById(R.id.edRegisterUserName);
        registrationEmail = findViewById(R.id.edRegisterEmail);
        registrationPassword = findViewById(R.id.edRegisterPassword);
        registrationConfirmPassword = findViewById(R.id.edRegisterConfirmPassword);
        registrationButton = findViewById(R.id.btnRegisterButton);

        registrationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName = registrationUserName.getText().toString();
                email = registrationEmail.getText().toString();
                password = registrationPassword.getText().toString();
                confirmPassword = registrationConfirmPassword.getText().toString();
                emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
                passwordPattern = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[@#$%^&+=!])(?=\\S+$).{4,}$";

                validationProcess();
            }
        });

    }

    public void validationProcess() {
        if (TextUtils.isEmpty(userName) && TextUtils.isEmpty(email) && TextUtils.isEmpty(password) && TextUtils.isEmpty(confirmPassword)) {
            Toast.makeText(getApplicationContext(), "Please fill all details", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(userName)) {
            Toast.makeText(getApplicationContext(), "Username field is Empty", Toast.LENGTH_SHORT).show();
        } else if (userName.length() < 3) {
            registrationUserName.setError("Username must be at least 3 letters long");
        } else if (userName.length() > 12) {
            registrationUserName.setError("Username must be less than 12 letters");
        } else if (TextUtils.isEmpty(email)) {
            Toast.makeText(getApplicationContext(), "Email field is Empty", Toast.LENGTH_SHORT).show();
        } else if (!email.matches(emailPattern)) {
            registrationEmail.setError("Invalid Email Address");
            Toast.makeText(getApplicationContext(), "Email must contain at least 1 Uppercase letter, " + "1 lowercase letter, 1 number & 1 special character", Toast.LENGTH_SHORT).show();
        } else if (email.matches(emailPattern)) {
            registrationEmail.setError(null);
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(), "Password field is Empty", Toast.LENGTH_SHORT).show();
        } else if (!password.matches(passwordPattern)) {
            registrationPassword.setError("Invalid Password");
            Toast.makeText(getApplicationContext(), "Password must contain at least 1 Uppercase letter, " + "1 lowercase letter, 1 number & 1 special character", Toast.LENGTH_SHORT).show();
        } else if (password.matches(passwordPattern)) {
            registrationPassword.setError(null);
        } else if (password.compareTo(confirmPassword) == 0) {
            registrationPassword.setError(null);
        } else if (password.compareTo(confirmPassword) != 0) {
            registrationPassword.setError("Passwords does not match");
        } else if (TextUtils.isEmpty(confirmPassword)) {
            Toast.makeText(getApplicationContext(), "Confirm Password field is Empty", Toast.LENGTH_SHORT).show();
        }
    }
}

Solution

  • As I was new to boolean methods, so did'nt know much about them but found a video on youtube (HOW TO) and solved the problem. I wasn't returning any false value so all the toasts were being interrupted and also some conditions were not working.

    changed the void method to boolean.

    package com.example.foodeezfavourite;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class RegistrationActivity extends AppCompatActivity {
    
        EditText registrationEmail, registrationPassword, registrationConfirmPassword, registrationUserName;
        Button registrationButton;
        String userName, email, password, confirmPassword, emailPattern, passwordPattern;
        boolean dataRecorded;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_registration);
    
            registrationUserName = findViewById(R.id.edRegisterUserName);
            registrationEmail = findViewById(R.id.edRegisterEmail);
            registrationPassword = findViewById(R.id.edRegisterPassword);
            registrationConfirmPassword = findViewById(R.id.edRegisterConfirmPassword);
            registrationButton = findViewById(R.id.btnRegisterButton);
    
            registrationButton.setOnClickListener(view -> {
                userName = registrationUserName.getText().toString();
                email = registrationEmail.getText().toString();
                password = registrationPassword.getText().toString();
                confirmPassword = registrationConfirmPassword.getText().toString();
                emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
                passwordPattern = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[@#$%^&+=!])(?=\\S+$).{8,}$";
    
                dataRecorded = isAllEmpty();
    
                if (dataRecorded == true) {
                    Toast.makeText(getApplicationContext(), "Data Recorded", Toast.LENGTH_SHORT).show();
                } else {
                }
            });
        }
    
        public Boolean isAllEmpty() {
            if (TextUtils.isEmpty(userName) && TextUtils.isEmpty(email) && TextUtils.isEmpty(password) && TextUtils.isEmpty(confirmPassword)) {
                Toast.makeText(getApplicationContext(), "Please fill all details", Toast.LENGTH_SHORT).show();
                return false;
            } else if (TextUtils.isEmpty(userName)) {
                Toast.makeText(getApplicationContext(), "Username field is Empty", Toast.LENGTH_SHORT).show();
                return false;
            } else if (userName.length() < 3) {
                registrationUserName.setError("Username must be at least 3 letters long");
                return false;
            } else if (userName.length() > 12) {
                registrationUserName.setError("Username must be less than 12 letters");
                return false;
            } else if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Email field is Empty", Toast.LENGTH_SHORT).show();
                return false;
            } else if (!email.matches(emailPattern)) {
                registrationEmail.setError("Invalid Email Address");
                Toast.makeText(getApplicationContext(), "Email must contain at least 1 Uppercase letter, " + "1 lowercase letter, 1 number & 1 special character", Toast.LENGTH_SHORT).show();
                return false;
            } else if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Password field is Empty", Toast.LENGTH_SHORT).show();
                return false;
            } else if (!password.matches(passwordPattern)) {
                registrationPassword.setError("Invalid Password");
                Toast.makeText(getApplicationContext(), "Password must contain at least 1 Uppercase letter, " + "1 lowercase letter, 1 number, 1 special character & must be 8 characters long ", Toast.LENGTH_SHORT).show();
                return false;
            } else if (TextUtils.isEmpty(confirmPassword)) {
                Toast.makeText(getApplicationContext(), "Confirm Password field is Empty", Toast.LENGTH_SHORT).show();
                return false;
            } else if (password.compareTo(confirmPassword) != 0) {
                registrationPassword.setError("Passwords does not match");
                return false;
            } else {
                return true;
            }
        }
    }