Iterating through viewgroup

I get the view count and then use that as a counter to call getChildAt(int index)


This question may have been long answered, but I wrote this recursive function to set onClickListeners for any buttons I find in my layout, but it could be repurposed:

private void recurseViews(ViewGroup v) {
    View a;
    boolean isgrp = false;
    for(int i = 0; i < v.getChildCount(); i++) { //attach listener to all buttons
        a = v.getChildAt(i);
        if(a instanceof ViewGroup) setcl((ViewGroup) a);
        else if(a != null) {
            //do stuff with View a
        }
    }
    return;
}

EDIT: Casting a View as ViewGroup does not raise an exception as had I previously thought, so the code is much shorter now

Tags:

Java

Android