0

I have TextInputLayout in login, when i click on login button if username is empty then show error same as with password when password is empty then show error but if username is empty and password is non empty at that time error is not shown so how can i solve this problem? My Code is like below!!!

@Override
public void onClick(View v) {
    Intent mIntent;
    switch (v.getId()) {
        case R.id.txtSignIn:
            hideKeyboard(mActivity, v);

            if (etUserName.getText().toString().equals("")) {
                etUserName.requestFocus();
                etUserName.setCursorVisible(true);
                tilPassword.setErrorEnabled(false);
                tilUserName.setErrorEnabled(true);
                tilUserName.setError(getString(R.string.username_required));
            } else if (etPassword.getText().toString().equals("")) {
                etPassword.requestFocus();
                etPassword.setCursorVisible(true);
                tilUserName.setErrorEnabled(false);
                tilPassword.setErrorEnabled(true);
                tilPassword.setError(getString(R.string.password_required));
            } else {
                tilUserName.setErrorEnabled(false);
                tilPassword.setErrorEnabled(false);
                showToast(mActivity, getString(R.string.successfully_login), 0);
                mIntent = new Intent(SignIn.this, DashBoard.class);
                startActivity(mIntent);
            }
            break;
        case R.id.txtSignUp:
            mIntent = new Intent(mActivity, SignUp.class);
            startActivity(mIntent);
            break;
        case R.id.txtForgotPassword:
            mIntent = new Intent(mActivity, ForgotPassword.class);
            startActivity(mIntent);
            break;
    }
    try {
    } catch (Exception e) {
        LOGD(getClass().getSimpleName(), e.getMessage());
        LOGE(getClass().getSimpleName(), e.getMessage());
    }
}
  • set rules for both of them if(!strUsername.isEmpty() && strPassw.isEmpty()) and so on... – Iqbal Rizky Jun 02 '16 at 05:52
  • Are you trying to instant-run the project every time or not? – Dhruv Jun 02 '16 at 06:09
  • Try to rebuild your project from `Build` > `Rebuild project`. For more information about instant run please see this [link](http://stackoverflow.com/a/33905037/2078074). – Dhruv Jun 02 '16 at 06:16
  • The Solutution of an above problem is tilUserName.setError(null); tilEmail.setError(null); –  Jun 02 '16 at 08:56

1 Answers1

0

Use isEmpty() method since null and "" <- this is not equal you cannot derive if the EditText is empty or not .

Something like this below will pop an error on the screen is either password or username field is empty

if (etUserName.isEmpty() && etPassword.isEmpty()) {
          <your code if nothing is entered>
}

For additional help check this out : Check if EditText is empty.

Community
  • 1
  • 1
Newbee
  • 45
  • 1
  • 9
  • So what is the result ? did you get it TextUtils is different from EditTexts isEmpty method TextUtils require you to Explicitly specify the CharSequesnce. – Newbee Jun 02 '16 at 06:03