-2

I have an android studio project for a personal app that has a simple password affixed to it in order to authenticate that I'm the one using it. My code is as follows:

public class StartActivity extends AppCompatActivity {
    FloatingActionButton authenticate;
    EditText passwordInput;

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

        authenticate = (FloatingActionButton) findViewById(R.id.authenticateCheck);
        passwordInput = (EditText) findViewById(R.id.inputPass);

        authenticate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(passwordInput.getText().toString() == "Magic") {
                    Intent i = new Intent(StartActivity.this, LilyMainInterface.class);
                    startActivity(i);
                } else {
                    passwordInput.setText("");
                }
            }
        });
    }
}

I've imported all of the necessary classes, but none of it is working. I don't see any problems and when I remove the if statement, the code successfully opens the correct activity. Other than that, it keeps considering the passwords as not matching.

Edit: I changed the

==

to

.equals()

as was said in a comment. It worked, however the reason why I wasn't doing it before was because android studio gives me a message saying that:

String values are compared with '==', not 'equals()'.

Why does .equals() work, even though android studio expressly states otherwise?

  • You are not getting the text from your edittext `passwordInput` to match with. Replace `if(passwordInput.toString() == "Magic")` with `if(passwordInput.getText..toString() == "Magic")`. – Haris ali Apr 30 '17 at 18:50

1 Answers1

0

You need to match string from edittext like:

  passwordInput = (EditText) findViewById(R.id.inputPass);

  if(passwordInput.getText().toString().equals("Magic")) {
        Intent i = new Intent(StartActivity.this, LilyMainInterface.class);
        startActivity(i);
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62