I have validation for editText
. If the editText
field is empty it should fail validation and stop the user moving on to another Activity
,
I faced a similiar problem. In my case, I had a lot of Edittexts to verify, some them was child of another layouts. Tip: all views must be in same root view. So, to be simple:
...
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.viewGroup);
checkFieldsRequired(viewGroup);
...
public boolean checkFieldsRequired(ViewGroup viewGroup){
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
checkFieldsRequired((ViewGroup) view);
else if (view instanceof EditText) {
EditText edittext = (EditText) view;
if (edittext.getText().toString().trim().equals("")) {
edittext.setError("Required!");
}
}
}
return false;
}