0

I want to launch different activities depending on the values of 2 EditText fields on the start activity, one should start for admins, the other for all other users.

I think you 'll understand better seeing the code I 've done so far:

package com.example.xxx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;

public class MainScreenActivity extends Activity {

    EditText USERNAME, PASSWORD;
    String username, password;
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        USERNAME = (EditText) findViewById(R.id.username);
        PASSWORD = (EditText) findViewById(R.id.password);

        // Button
        btnLogin = (Button) findViewById(R.id.btnLogin);


        // login click event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // Launching next activity
                if (USERNAME="admin" && PASSWORD="1234") {
                    Intent i = new Intent(getApplicationContext(), admin.class);
                    startActivity(i);

                } else {

                Intent i = new Intent(getApplicationContext(), user_form.class);
                startActivity(i);
                }
            }
        });
    }
}

The error is at the if statement, on PASSWORD="1234" says "Incompatible operand types EditText and String". I also tried password="1234" and password.toString="1234" but no luck.

I'm not even sure if I'm to use 'USERNAME' and 'PASSWORD' or 'username' and 'password'. How can I make this work?

EDIT: Ok now the code is correct, having if (USERNAME.getText().equals("admin") && PASSWORD.getText().equals("1234")), but the application always opens the user_form activity, and not the admin, even though I input the specific values. Where is the mistake?

gkri
  • 1,627
  • 3
  • 18
  • 27
  • 3
    Use `String.equals` instead of `==` for comparing strings – ρяσѕρєя K Feb 26 '15 at 13:58
  • @ρяσѕρєяK Tried that too. Seems it works for PASSWORD, but on USERNAME says "admin cannot be resolved to a variable". Can I use both these structures? EDIT: No, I cannot use them both: `if (USERNAME="admin" && PASSWORD.equals(1234))` has "The operator && is undefined for the argument type(s) String, boolean" EDIT: Ok, my bad, `if (USERNAME.equals("admin") && PASSWORD.equals(1234))` seems to work, thank you :) – gkri Feb 26 '15 at 14:02
  • 2
    USERNAME and PASSWORD are not strings they are EditText objects you need to call USERNAME.getText().equals("admin"). – Ben Feb 26 '15 at 14:05
  • do it as `if (USERNAME.getText().toString().equals("admin") && PASSWORD.getText().toString().equals(("1234")) {` – ρяσѕρєя K Feb 26 '15 at 14:06
  • Well it's not working... The code seems correct, but the application always opens the second activity ("user_form.class") and not the first ("admin.class"). – gkri Feb 26 '15 at 14:30
  • Do the comparison as ρяσѕρєя K desribed: do not forget the toString()! – Christopher Feb 26 '15 at 14:34
  • Oh snap, correct!! Sorry for the rush! – gkri Feb 26 '15 at 14:41

0 Answers0