82

If for example I have defined a root linear layout whose orientation is vertical:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_root"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:orientation="vertical"

    <!-- I would like to add content here dynamically.-->

</LinearLayout>

Inside the root linear layout, I would like to add multiple child linear layouts, each of the child linear layout orientation is horizontal. With all these I could end up with a table like output.

For example root with child layout such as:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/my_root"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:orientation="vertical"

    <!-- 1st child (1st row)-->
    <LinearLayout 
        ...
       android:orientation="horizontal">

          <TextView .../>
          <TextView .../>
          <TextView .../>
    </LinearLayout>

     <!-- 2nd child (2nd row)-->
     ...
</LinearLayout>

Since the number of child linear layouts and their contents are quite dynamic, I've decide to add content to the root linear layout programmatically.

How can the second layout be added to the first programmatically, which could also set all the layout attributes for each child and add more other elements inside child?

4 Answers 4

111

In your onCreate(), write the following

LinearLayout myRoot = (LinearLayout) findViewById(R.id.my_root);
LinearLayout a = new LinearLayout(this);
a.setOrientation(LinearLayout.HORIZONTAL);
a.addView(view1);
a.addView(view2);
a.addView(view3);
myRoot.addView(a);

view1, view2 and view3 are your TextViews. They're easily created programmatically.

3
  • 5
    Anyone know why this is? Why can't I just do this.addView(view1) if this extends LinearLayout?
    – MegaWidget
    Commented Sep 23, 2016 at 20:07
  • Nice, but when should I use this, and when should I use TableLayout instead? Is it just a matter of personal taste?
    – Zerato
    Commented Mar 7, 2018 at 11:56
  • we must add linear layout into dynamic horizontalScrollView too. tnx : ) Commented May 26, 2018 at 9:35
79
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
View child = getLayoutInflater().inflate(R.layout.child, null);
layout.addView(child);
2
  • 1
    If we do like this. How do we manage to handle click events inside "layout.child"?
    – Malindu
    Commented Nov 27, 2016 at 10:31
  • 3
    @Warapitiy Use child.findViewById to get the element and set onclick listener.
    – Choxmi
    Commented Mar 4, 2017 at 4:58
5

You can achieve LinearLayout cascading like this:

LinearLayout root = (LinearLayout) findViewById(R.id.my_root);    
LinearLayout llay1 = new LinearLayout(this);    
root.addView(llay1);
LinearLayout llay2 = new LinearLayout(this);    
llay1.addView(llay2);
2

I found more accurate way to adding views like linear layouts in kotlin (Pass parent layout in inflate() and false)

val parentLayout = view.findViewById<LinearLayout>(R.id.llRecipientParent)
val childView = layoutInflater.inflate(R.layout.layout_recipient, parentLayout, false)
parentLayout.addView(childView)

Not the answer you're looking for? Browse other questions tagged or ask your own question.