Search code examples
androidreflectionviewgetelementsbytagname

Android: getElementsByTagName equivalent?


Is it possible to search the hierarchy of View and get Views of particular type?

Equivalently to getElementsByTagName in JS

defined like this:

public ArrayList<T> getElementsByTagName(View v, Class<T>){
    ....
}

called similar to this:

ArrayList<TextView> labels = getElementsByTagName(parentView, TextView.class);

Solution

  • ViewGroup has getChildCount() and getChildAt() so you can traverse the tree and filter the children you want.