Getting child elements from LinearLayout

Cody picture Cody · Jul 7, 2011 · Viewed 64.3k times · Source

Is there a way to obtain a child element of a LinearLayout? My code returns a view (linearlayout), but I need to get access to specific elements inside of the layout.

Any suggestions?

(Yes, I know I could use findViewById, but I am creating the layouts/children in java - not XML.)

Answer

Aleks G picture Aleks G · Jul 7, 2011

You can always do something like this:

LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
    v = layout.getChildAt(i);
    //do something with your child element
}