public static EditText traverseEditTexts(ViewGroup v) {
EditText invalid = null;
for (int i = 0; i < v.getChildCount(); i++) {
Object child = v.getChildAt(i);
if (child instanceof EditText) {
EditText e = (EditText) child;
if (e.getText().length() == 0) // Whatever logic here to
// determine if valid.
{
return e; // Stops at first invalid one. But you could add
// this to a list.
}
} else if (child instanceof ViewGroup) {
invalid = traverseEditTexts((ViewGroup) child); // Recursive
// call.
if (invalid != null) {
break;
}
}
}
return invalid;
}
Use the Above function to search a edit text in the Activty
private boolean validateFields() {
ViewGroup MainLayout = (ViewGroup) findViewById(R.id.LoginParentLayout);
EditText emptyText = FormValidation.traverseEditTexts(MainLayout);
if (emptyText != null) {
String field = emptyText.getHint().toString();
showToast(field + " field cannot be empty.");
emptyText.requestFocus(); // Scrolls view to this field.
}
return emptyText == null;
}
No comments:
Post a Comment