-3

This is an android java code. eEmi, eLoss, eMonth, eMembers are EditText and the app has submit and reset button. The problem is that the else part in the code is always executing. What I want here is that the user should not leave any EditText empty, if he leaves anyone empty, it should display a toast notification. What is wrong in the code? and is there any other way to do this efficiently?

eEmi = (EditText) findViewById(R.id.emi);
    eMembers = (EditText) findViewById(R.id.members);
    eLoss = (EditText) findViewById(R.id.loss);
    eMonth = (EditText) findViewById(R.id.month);
    submit = (Button) findViewById(R.id.Bsubmit);
    reset = (Button) findViewById(R.id.Breset);

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (eEmi.getText().toString() == null
                    && eMembers.getText().toString() == null
                    && eLoss.getText().toString() == null
                    && eMonth.getText().toString() == null) {
                emi = Double.valueOf(eEmi.getText().toString());
                members = Double.valueOf(eMembers.getText().toString());
                loss = Double.valueOf(eLoss.getText().toString());
                month = Double.valueOf(eMonth.getText().toString());
                Intent submit = new Intent(MainActivity.this,
                        ResultActivity.class);
                submit.putExtra("emi", emi);
                submit.putExtra("members", members);
                submit.putExtra("loss", loss);
                submit.putExtra("month", month);
                startActivity(submit);
            } else {
                Toast.makeText(MainActivity.this, R.string.invalid, Toast.LENGTH_SHORT).show();
            }
        }
    });
Kush Grover
  • 363
  • 1
  • 6
  • 16

1 Answers1

0

The Strings will never be null, just empty.

Use the String isEmpty() method:

  if (!eEmi.getText().toString().isEmpty()
                && !eMembers.getText().toString().isEmpty()
                && !eLoss.getText().toString().isEmpty()
                && !eMonth.getText().toString().isEmpty()) {
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137