Getting child elements from LinearLayout
Asked Answered
P

7

71

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.)

Pescara answered 7/7, 2011 at 19:3 Comment(0)
C
109

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
}
Coating answered 7/7, 2011 at 19:6 Comment(1)
Thanks. What worked was: TextView tv = (TextView)((LinearLayout )v).getChildAt(0);Pescara
P
22

I think this could help: findViewWithTag()

Set TAG to every View you add to the layout and then get that View by the TAG as you would do using ID

Plath answered 7/7, 2011 at 19:17 Comment(0)
H
7
LinearLayout layout = (LinearLayout)findViewById([whatever]);
for(int i=0;i<layout.getChildCount();i++)
    {
        Button b =  (Button)layout.getChildAt(i)
    }

If they are all buttons, otherwise cast to view and check for class

View v =  (View)layout.getChildAt(i);
if (v instanceof Button) {
     Button b = (Button) v;
}
Higginbotham answered 18/4, 2015 at 19:15 Comment(0)
F
5

I would avoid statically grabbing an element from the view's children. It might work now, but makes the code difficult to maintain and susceptible to breaking on future releases. As stated above the proper way to do that is to set the tag and to get the view by the tag.

Fatigue answered 7/7, 2011 at 23:44 Comment(0)
V
1

You can do like this.

ViewGroup layoutCont= (ViewGroup) findViewById(R.id.linearLayout);
getAllChildElements(layoutCont);
public static final void getAllChildElements(ViewGroup layoutCont) {
    if (layoutCont == null) return;

    final int mCount = layoutCont.getChildCount();

    // Loop through all of the children.
    for (int i = 0; i < mCount; ++i) {
        final View mChild = layoutCont.getChildAt(i);

        if (mChild instanceof ViewGroup) {
            // Recursively attempt another ViewGroup.
            setAppFont((ViewGroup) mChild, mFont);
        } else {
            // Set the font if it is a TextView.

        }
    }
}
Vladimir answered 26/3, 2013 at 9:48 Comment(0)
L
1

The solution in Kotlin version will be:

val layout: LinearLayout = setupLayout()
        val count = layout.childCount
        var v: View? = null
        for (i in 0 until count) {
            v = layout.getChildAt(i)
            //do something with your child element
        }
Longdrawnout answered 29/6, 2020 at 12:21 Comment(0)
N
0

One of the very direct ways is to access the child views with their ids.

view.findViewById(R.id.ID_OF_THE_CHILD_VIEW)

here view is the parent view.

So for instance your child view is an imageview, with id, img_profile, what you can do is:

ImageView imgProfile = view.findViewById(R.id.img_profile)
Nipha answered 9/12, 2019 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.