Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking username and password in Android

Tags:

java

android

I have a username and password field and now i need to check and redirect him to the next page in Android.

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);   
         setContentView(R.layout.main);
         final EditText loginText = (EditText) findViewById(R.id.widget44);
         final EditText loginPassword = (EditText) findViewById(R.id.widget47);
         final Button button = (Button) findViewById(R.id.widget48);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                 Intent myIntent = null;
                 if(loginText.getText().equals("admin") && 
loginPassword.getText().equals("admin")) {
                  System.out.println("Entering");
                  myIntent = new Intent(view.getContext(), Page1.class);
                 } else {

                 }
                 startActivity(myIntent);
                }
            });
        }

Temp now i am checking by hardcoding the values, but this too does not work for me. Why? I usually check in Java like this, why does it not accept me the same way in Android

like image 271
theJava Avatar asked Jan 24 '11 09:01

theJava


2 Answers

I think it's because EditText#getText() returns an Editable object. Try

if(loginText.getText().toString().equals("admin") && 
    loginPassword.getText().toString().equals("admin")) {
    ...
}
like image 120
alex.zherdev Avatar answered Oct 04 '22 14:10

alex.zherdev


Don't you need to put toString() to your textelements?
Like this:

if(loginText.getText().toString().equals("admin") && 
loginPassword.getText().toString().equals("admin")) {
...

Edit: neutrino was faster (+1) :)

like image 44
Beasly Avatar answered Oct 04 '22 13:10

Beasly