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
This code was works.
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: