40

I’m working on a game for Android. To help implement it, my idea is to create a subclass of a view. I would then insert several instances of this class as children of the main view. Each instance would handle detecting when it was pressed (via OnTouchListener).

The problem I’m having now is how do I loop through all these sub-views so I can read their statuses and process them? (I.e. when they all reach a certain state something should happen).

Or is there a better way to have several objects on the screen that respond to touch and whose status I can check?

MikeL
  • 2,756
  • 2
  • 23
  • 41
Slapout
  • 3,759
  • 5
  • 40
  • 61

6 Answers6

70

I have made a small example of a recursive function:

public void recursiveLoopChildren(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            final View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                recursiveLoopChildren((ViewGroup) child);
                // DO SOMETHING WITH VIEWGROUP, AFTER CHILDREN HAS BEEN LOOPED
            } else {
                if (child != null) {
                    // DO SOMETHING WITH VIEW
                }
            }
        }
    }

The function will start looping over al view elements inside a ViewGroup (from first to last item), if a child is a ViewGroup then restart the function with that child to retrieve all nested views inside that child.

MikeL
  • 2,756
  • 2
  • 23
  • 41
Tobrun
  • 18,291
  • 10
  • 66
  • 81
  • 2
    Any particular reason you have chosen `for (int i = parent.getChildCount() - 1; i >= 0; i--)` instead of `for (int i = 0; i < parent.getChildCount(); i++)`? – Peter Prokop Aug 23 '17 at 12:15
  • @PeterProkop I personally like this loop more. It works well even if you remove one of the element of the loop, during the loop. – Sira Lam Nov 07 '17 at 04:47
  • @Karacago WebView is a ViewGroup, therefore you can add the `if (child instanceof WebView)` condition before `if (child instanceof ViewGroup)`. – MewX Dec 12 '17 at 11:48
26

@jqpubliq Is right but if you really want to go through all Views you can simply use the getChildCount() and getChildAt() methods from ViewGroup. A simple recursive method will do the rest.

TooCool
  • 10,598
  • 15
  • 60
  • 85
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
2

Android loop through all views

The alternative

public static void recursivelyFindChildren(View view) {
    if (view instanceof ViewGroup) {
        //ViewGroup
        ViewGroup viewGroup = (ViewGroup)view;
        
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            recursivelyFindChildren(viewGroup.getChildAt(i));
        }

    } else {
        //View
    }

}

Or you can return something you can use the next approach

@Nullable
private static WebView recursivelyFindWebView(View view) {
    if (view instanceof ViewGroup) {
        //ViewGroup
        ViewGroup viewGroup = (ViewGroup)view;

        if (!(viewGroup instanceof WebView)) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                WebView result = recursivelyFindWebView(viewGroup.getChildAt(i));

                if (result != null) {
                    return result;
                }
            }
        } else {
            //WebView

            WebView webView = (WebView)viewGroup;
            return webView;
        }
    }
    return null;
}
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
1

Try this. Takes all views inside a parent layout & returns an array list of views.

public List<View> getAllViews(ViewGroup layout){
        List<View> views = new ArrayList<>();
        for(int i =0; i< layout.getChildCount(); i++){
            views.add(layout.getChildAt(i));
        }
        return views;
    }
NJY404
  • 349
  • 3
  • 14
1

In Kotlin, I am using an extension function on ViewGroup:

//call forOneChild for the ViewGroup itself, and all sub Views/ViewGroups
fun ViewGroup.forAllChildren(forOneChild: (v: View) -> Unit) {
    forOneChild(this)
    for (cx in 0 until childCount) {
        val child = getChildAt(cx)
        if (child is ViewGroup)
            child.forAllChildren(forOneChild)
        else
            forOneChild(child)
    }
}

I can then call this (for any ViewGroup, from anywhere).

Example:

        //set colors for sub views
        val colorHi = Color.parseColor("#006400") //DarkGreen
        val colorLo = Color.parseColor("#90EE90") //LightGreen

        binding.root.forAllChildren { v ->
            when (v) {
                is CheckBox -> v.buttonTintList = ColorStateList.valueOf(colorHi)
                is Spinner -> v.setPopupBackgroundDrawable(ColorDrawable(colorLo))
            }
        }
Free Dorfman
  • 341
  • 1
  • 3
  • 13
0

Using Views sounds like its going to be brutally difficult to render anything well if there is movement. You probably want to be drawing to a Canvas or using OpenGL unless you're doing something really static. Here's a great talk from last years I/O conference on making Android games. Its kind of long and you can skip about 15 minutes in. Also the source is available. That should give you a good idea of ways to go about things

jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • Yeah, I made need to do it a different way, but I'm going to try this first. It's a casual game. – Slapout Apr 14 '10 at 02:29