How to iterate through a view’s elements

I’ve done something similar in some code I don’t have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of “layout”):

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
boolean success = formIsValid(layout);

public boolean formIsValid(LinearLayout layout) {
    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        if (v instanceof EditText) {
            //validate your EditText here
        } else if (v instanceof RadioButton) {
            //validate RadioButton
        } //etc. If it fails anywhere, just return false.
    }
    return true;
}

Leave a Comment