I am trying to make a login screen for my android app. I enter to correct username and password and I get the toast message saying I have the wrong username or password. I've tried everything I can think of to try and fix this and thought I would ask for some help on here.

public void welcomeScreen(View view){
        String userName1 = "student";
        String password1 = "school";
        Intent intent = new Intent(this, WelcomeScreen.class);
        EditText editText1 = (EditText) findViewById(R.id.editText1);
        String userName = editText1.getText().toString();

        EditText editText2 = (EditText) findViewById(R.id.editText2);
        String password = editText2.getText().toString();

        if(userName != userName1 || password != password1){
            Context context = getApplicationContext();
            CharSequence text = "Incorrect Username or Password!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

        else startActivity(intent);
    }

Dani AI

Generated

The root cause in 's example is using != to compare String objects. In Java ==/!= checks reference identity, not text content. As noted, use equals() (or a null-safe variant) so the entered text is compared by value rather than by object reference.

A simple, safe pattern (showing different variable names from the original) is:

String expectedUser = "student";
String expectedPass = "school";

String enteredUser = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
String enteredPass = ((EditText)findViewById(R.id.editText2)).getText().toString();

if (!expectedUser.equals(enteredUser) || !expectedPass.equals(enteredPass)) {
    Toast.makeText(this, "Incorrect username or password", Toast.LENGTH_SHORT).show();
    return;
}
startActivity(new Intent(this, WelcomeScreen.class));

Troubleshooting checklist if problems persist:

  • Confirm the layout ids match the ones used in findViewById.
  • Verify the click handler is actually wired (android:onClick or setOnClickListener).
  • Trim input (or at least username) to remove accidental spaces; be cautious trimming passwords if exact whitespace matters.
  • Use TextUtils.equals(a,b) or put the constant on the left ("student".equals(enteredUser)) to avoid NPEs.
  • Temporarily log or toast the entered username (never log plain passwords in production) to confirm values being read.
  • Create the Intent after validation and ensure the target activity is declared in AndroidManifest.

Security note: never ship hardcoded credentials in real apps. For production use proper authentication (server-side auth, OAuth, tokens) and secure storage (encrypted preferences or Android AccountManager) rather than plaintext checks inside the client.

Guess you already solve it since post is marked as solved, but for others never do compare strings as numeric values with == or != always use strin1.equals(string2)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.