-3

I created simple android form in which I get username and password and in that If I Enter username and password is deepak then also it goes to else condition. But my if condition is always false in any situation and I don't know why.

package com.example.dac.simple_intent_form;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button button;
EditText email, password;

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

    button = (Button) findViewById(R.id.button);
    email = (EditText) findViewById(R.id.eemail);
    password = (EditText) findViewById(R.id.epassword);

    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    String ema = email.getText().toString();
    String pass = password.getText().toString();

    if (ema == "deepak" && pass == "deepak")
    {
        Bundle b = new Bundle();
        b.putString("email", ema);
        b.putString("password", pass);
        Intent i = new Intent(getApplicationContext(),Activity2.class);
        i.putExtras(b);
        startActivity(i);
    }
    else
    {
        Toast.makeText(getApplicationContext(),"Plase Enter Right Username and Password",Toast.LENGTH_SHORT).show();
    }
}
}
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
  • 1
    don't use "==", use ema.equals("deepak") – Sujith Niraikulathan Aug 26 '16 at 10:50
  • Seems you got the answer but just in case if you're wondering why == is not working then, `==` checks the reference. `String a = "abc"` and `String b = "abc"` but `a==b` will give you false, coz the references inside memory of `String` `a` and `b` are both different. – Srujan Barai Aug 30 '16 at 12:03

5 Answers5

1

Use this

ema.equals("deepak") && pass.equals("deepak") in your if statement

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
0

Try use this

String ema = email.getText().toString();
    String pass = password.getText().toString();

    if (ema.toLowerCase().equals("deepak") && pass.toLowerCase().equals("deepak"))
    {


    }
Sabish.M
  • 2,022
  • 16
  • 34
0

Check the condition like this and it will work fine

if (ema.equals("deepak") && pass.equals("deepak")){}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Dinesh Saini
  • 335
  • 1
  • 12
0

if("deepak".equals(ema) && "deepak".equals(pass)) will do the job .This will also make sure objects ema and pass are not null and can avoid null check

-1

use .equals()

if (ema.equals("deepak") && pass.equals("deepak"))
{
    Bundle b = new Bundle();
    b.putString("email", ema);
    b.putString("password", pass);
    Intent i = new Intent(getApplicationContext(),Activity2.class);
    i.putExtras(b);
    startActivity(i);
}
Chirag Arora
  • 816
  • 8
  • 20