Search code examples
androidsqliteadmin

Print a toast if specific parameter is correct


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

            }
        });
    }
}

Solution

  • 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");
    }