Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting child elements from LinearLayout

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

like image 233
Cody Avatar asked Jul 07 '11 19:07

Cody


People also ask

How do you get a child of LinearLayout?

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.

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

What is the use of LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What is RelativeLayout and LinearLayout?

Android Layout TypesLinearLayout : 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.


1 Answers

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 } 
like image 161
Aleks G Avatar answered Sep 30 '22 02:09

Aleks G