Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Child view in a RelativeLayout

Tags:

android

layout

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?

like image 312
Lalit Chattar Avatar asked Oct 18 '11 12:10

Lalit Chattar


People also ask

Is a ViewGroup that displays child views in relative position?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

What's the difference between LinearLayout and RelativeLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.


2 Answers

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...
}
like image 151
Ovidiu Latcu Avatar answered Sep 19 '22 00:09

Ovidiu Latcu


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.

like image 41
Arnab Chakraborty Avatar answered Sep 22 '22 00:09

Arnab Chakraborty