0

I'm trying to print a toast msg if the username and password is equal to admin i try this code but the toast msg says it's error and the logcat doesn't show anything

code i tried

public class login extends AppCompatActivity {

    DatabaseHelper db;
    EditText username,password;
    Button login;

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

        db = new DatabaseHelper(this);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        login = findViewById(R.id.login);

        adminLogin();
    }
    public void toast(Context context, String string){
        Toast.makeText(this,string, Toast.LENGTH_SHORT).show();
    }
    public void adminLogin(){
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(username.getText().toString() == "admin" || password.getText().toString() == "admin"){

                    toast(login.this,"Done");

                }
                else{
                    toast(login.this,"Error");
                }

            }
        });
    }
}
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jsamol Sep 21 '19 at 18:40

1 Answers1

0

You should not use == to compare String in java, rather you should use .equals method on String. So your logic should be as follow:

if(username.getText().toString().equals("admin") || password.getText().toString().equals("admin")){
     toast(login.this,"Done");
}
Ali
  • 9,800
  • 19
  • 72
  • 152