Lalit Chattar
Lalit Chattar

Reputation: 2004

Get Child view in a RelativeLayout

I want to add one button in my activity that will return all child view of relative layout.

How can i get all child view of relative layout view?

Upvotes: 8

Views: 24590

Answers (2)

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72341

RelativeLayout extends ViewGroup which has the getChildCount() and getChildAt(int index) methods. So what you could try is:

for(int i = 0; i < relativeLayout.getChildCount(); i++) {
   View child = relativeLayout.getChildAt(i);
   // your processing...
}

Upvotes: 35

Arnab Chakraborty
Arnab Chakraborty

Reputation: 7472

Just the child count for the view and iterate over each of them. Something like this :

int childCount = myRelativeLayout.getChildCount();
for(int i = 0; i < childCount; i++) {
    View v = myRelativeLayout.getChildAt(i);
    // do whatever you want to with the view
}

Hope this helps.

Upvotes: 2

Related Questions