androidandroid-view

How to check if a view has children


Is there a way to check if a view has children? For example...

boolean bool = view.hasChildren(); //returns true if view has one or more children

I need to know this because I have an empty layout I'm adding new views to dynamically and I need to know if the layout is empty or not.


Solution

  • Is there a way to check if a view has children

    Assuming is a subclass of ViewGroup, you can use getChildCount(). E.g.

     public static boolean hasChildren(ViewGroup viewGroup) {
        return viewGroup.getChildCount() > 0;
     }