Home > Software design >  If ,else and else if statement are not working on button click in Android
If ,else and else if statement are not working on button click in Android

Time:12-26

I face a problem when I use if, else, and else if statements. I was making a button Login, there I used that type of code

  edtEditText = findViewById(R.id.username);
    edtEditText1 = findViewById(R.id.password);
    String email = edtEditText.getText().toString().trim();
    String password = edtEditText1.getText().toString().trim();
    
    btn = findViewById(R.id.loginbtn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (email.equals("")){
                Toast toast = Toast.makeText(getApplicationContext(), "Please Enter Email",Toast.LENGTH_SHORT);
                toast.show();
            }else if (password.equals("")) {
                Toast toast = Toast.makeText(getApplicationContext(), "Please Enter Password",Toast.LENGTH_SHORT);
                toast.show();
            }else {
                signIn();
            }
        }
    });

It works only a single line after this line.

 @Override
        public void onClick(View view) {

How can I solve this?

CodePudding user response:

You are using a closure, the String email is bounded to the value at runtime at setOnClickListener() and is probably the empty string.

You should do this:

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String email = edtEditText.getText().toString().trim();
            if (email.equals("")){

CodePudding user response:

    edtEditText = findViewById(R.id.username);
    edtEditText1 = findViewById(R.id.password);
    
    btn = findViewById(R.id.loginbtn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String email = edtEditText.getText().toString().trim();
            String password = edtEditText1.getText().toString().trim();
            if (email.equals("")){
                Toast toast = Toast.makeText(getApplicationContext(), "Please Enter Email",Toast.LENGTH_SHORT);
                toast.show();
            }else if (password.equals("")) {
                Toast toast = Toast.makeText(getApplicationContext(), "Please Enter Password",Toast.LENGTH_SHORT);
                toast.show();
            }else {
                signIn();
            }
        }
    });

CodePudding user response:

This code was works.

Button signup = (Button) findViewById(R.id.signup1);
        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = edtEditText.getText().toString().trim();
                String password = edtEditText1.getText().toString().trim();
                if (TextUtils.isEmpty(email)) {
                    edtEditText.setError("Email is Required");
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    edtEditText1.setError("Password is Required");
                    return;
                }
                if (password.length() < 5) {
                    edtEditText1.setError("Password will be Minimum 6 Character");
                    return;
                }


                createAccount();
                sendEmailVerification();

            }
  • Related