William Willi William Willi - 4 months ago 20
Android Question

Stop an action if ".setError" encounters

This is my android code, here is a sign up page with 'name','mobile no','email','password','confirm password' and i have added setError method for any editText is left blank, but what happens is when i click the "Sign up" button android shows the error message but it never waits for the user, it directly does the OnClick action for the sign up button.

what i need is when the user hits "sign up" it should check for the blank space if there is any blank space the OnClick action should not proceed.

Here is my android code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final EditText etMobileNo = (EditText) findViewById(R.id.etMobileNo);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final EditText etConfirmPassword = (EditText) findViewById(R.id.etConfirmPassword);
final Button bSignUp = (Button) findViewById(R.id.bSignUp);

bSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = etName.getText().toString;
email = etEmail.getText().toString();
mobile = etMobileNo.getText().toString();
password = etPassword.getText().toString();
confirmpassword = etConfirmPassword.getText().toString();



CheckMissing(); //Check for Missing entries and Password Matches.



Response.Listener<String> responseListener = new Response.Listener<String>() {
private ProgressDialog loading;
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
loading = ProgressDialog.show(SignupActivity.this, "Thank You...", "Registering Now...", false, false);
if (success) {
loading.dismiss();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
SignupActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};

SignupRequest signupRequest = new SignupRequest(name, email, mobile, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(SignupActivity.this);
queue.add(signupRequest);
}
});
}

public void CheckMissing() {
EditText name = (EditText) findViewById(R.id.etName);
if (name.getText().toString().isEmpty()) {
name.setError("You need to enter name");
}
EditText email = (EditText) findViewById(R.id.etEmail);
if (email.getText().toString().isEmpty()) {
email.setError("You need to enter Email");
}

EditText mobile = (EditText) findViewById(R.id.etMobileNo);
if (mobile.getText().toString().isEmpty()) {
mobile.setError("You need to enter mobile");
}
EditText password = (EditText) findViewById(R.id.etPassword);
String Pass = password.getText().toString();
if (password.getText().toString().isEmpty()) {
password.setError("You need to enter password");
}
EditText confirmpassword = (EditText) findViewById(R.id.etConfirmPassword);
String Conpass = confirmpassword.getText().toString();
if (confirmpassword.getText().toString().isEmpty()) {
confirmpassword.setError("You need to enter confirmpassword");
}
if (!Pass.equals(Conpass)) {
confirmpassword.setError("Password Doesnt Match");
}
}
}

Answer

Add a boolean return type in CheckMissing():

public boolean CheckMissing() {
    boolean shouldProceed = true;
    EditText name = (EditText) findViewById(R.id.etName);
    if (name.getText().toString().isEmpty()) {
        name.setError("You need to enter name");
        shouldProceed = false;
    }
    EditText email = (EditText) findViewById(R.id.etEmail);
    if (email.getText().toString().isEmpty()) {
        email.setError("You need to enter Email");
        shouldProceed = false;
    }

    EditText mobile = (EditText) findViewById(R.id.etMobileNo);
    if (mobile.getText().toString().isEmpty()) {
        mobile.setError("You need to enter mobile");
        shouldProceed = false;
    }
    EditText password = (EditText) findViewById(R.id.etPassword);
    String Pass = password.getText().toString();
    if (password.getText().toString().isEmpty()) {
        password.setError("You need to enter password");
        shouldProceed = false;
    }
    EditText confirmpassword = (EditText) findViewById(R.id.etConfirmPassword);
    String Conpass = confirmpassword.getText().toString();
    if (confirmpassword.getText().toString().isEmpty()) {
        confirmpassword.setError("You need to enter confirmpassword");
        shouldProceed = false;
    }
    if (!Pass.equals(Conpass)) {
        confirmpassword.setError("Password Doesnt Match");
        shouldProceed = false;
    }

    return shouldProceed;
}

Then in OnClickListener of sign up button:

if (!CheckMissing())
    return;