skip to Main Content

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?

3

Answers


  1. Chosen as BEST ANSWER

    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();
    
                }
    

  2. 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("")){
    
    Login or Signup to reply.
  3.     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();
                }
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search