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.)
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.
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.
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.
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With