0
  public void myClick1(View v){
    EditText txtUser = (EditText) findViewById(R.id.strUser);
    EditText txtPwd = (EditText)findViewById(R.id.strPwd);

  if (txtUser.equals("admin")){
        Toast.makeText(getApplicationContext(),"Authentication Pass", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(getApplicationContext(),"Not valid User ID/Password: " + txtUser.getText(), Toast.LENGTH_LONG).show();
}

No matter what i key in into text box, it always active the else statement. Why?

user3860946
  • 27
  • 1
  • 6

3 Answers3

1

You should use txtUser.getEditableText().toString() when comparing like this:

if ("admin".equals(txtUser.getEditableText().toString())){
    Toast.makeText(getApplicationContext(),"Authentication Pass", Toast.LENGTH_LONG).show();
}else{
    Toast.makeText(getApplicationContext(),"Not valid User ID/Password: " + txtUser.getText(), Toast.LENGTH_LONG).show();
azertiti
  • 3,150
  • 17
  • 19
0

You have to get the text of the TextView, like this:

    EditText txtUser = (EditText) findViewById(R.id.strUser);
    EditText txtPwd = (EditText) findViewById(R.id.strPwd);

    if ("admin".equals(txtUser.getText().toString())) {
        Toast.makeText(getApplicationContext(), "Authentication Pass", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),"Not valid User ID/Password: " + txtUser.getText(), Toast.LENGTH_LONG).show();
    }
Matthieu Coisne
  • 624
  • 1
  • 13
  • 25
-2

I'm not familiar with android atm, but have you tried:

txtUser.getText() == "admin"

Thanks for the comments (I was using C# approach), fixed code:

txtUser.getText().equals("admin")
farfanium
  • 1
  • 1
  • 1
    This is wrong for a couple reasons. One being [how to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – codeMagic Jun 12 '15 at 15:38