I am trying to check whether the user has enter his/her mobile number in the EditText on the button click event, but I am not able to check whether the EditText is empty or not when control comes to "editRegMobile". When I click on the button it doesn't show the message in the Toast. I also want to check the format of the mobile number i.e "+91 999999999" country code. I am able to check for other EditText but not for Mobile EditText.
xml code:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editRegMobile"
android:layout_gravity="center_horizontal"
android:background="@drawable/sign_in_up_textbox"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="35dp"
android:textSize="16sp"
android:letterSpacing="0.08"
android:singleLine="true" />
Java code:
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editRegName != null && TextUtils.isEmpty(editRegName.getText().toString().trim())) {
editRegName.setError("Required!");
editRegName.requestFocus();
} else if (editRegEmail != null && TextUtils.isEmpty(editRegEmail.getText().toString())) {
editRegEmail.setError("Required!");
editRegEmail.requestFocus();
} else if (!(editRegEmail != null && TextUtils.isEmpty(editRegEmail.getText().toString()))) {
if (!Patterns.EMAIL_ADDRESS.matcher(editRegEmail.getText().toString()).matches()) {
editRegEmail.setError("Invalid Format!");
editRegEmail.requestFocus();
}
} else if ((editRegMobile != null) && TextUtils.isEmpty(editRegMobile.getText().toString())) {
Toast.makeText(getApplicationContext(), "Please enter mobile number!", Toast.LENGTH_SHORT).show();
} else if (!(editRegMobile != null && TextUtils.isEmpty(editRegMobile.getText().toString()))) {
if (!Patterns.PHONE.matcher(editRegMobile.getText().toString()).matches()) {
Toast.makeText(getApplicationContext(), "Please enter valid mobile number!", Toast.LENGTH_SHORT).show();
}
}
}
});