4

I would like to inflate a LinearLayout with multiple instances of another LinearLayout. How can I do that? My problem is that I seem to always use the same instance and hence add that instance over and over again.

In short: What I need is a way to add new instances of a LinearLayout child to another LinearLayout parent.

Here is what I have done so far:

private void setupContainers() {
    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(MainActivity.LAYOUT_INFLATER_SERVICE);
    LinearLayout parentContainer = (LinearLayout)this.findViewById(R.id.parent_container);

    for (int i = 0; i < someNumber; i++) {

        LinearLayout childContainer = (LinearLayout) layoutInflater.inflate(R.layout.child_container, null);
        parentContainer.addView(childContainer);

    }
}
user2426316
  • 7,131
  • 20
  • 52
  • 83

1 Answers1

4

Try this:

for (int i = 0; i < someNumber; i++) {
    LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or any other layout params that suit your needs
    LinearLayout childContainer = new LinearLayout(this);
    parentLayout.addView(childContainer, params)
}

EDIT

Considering you need to use the content from XML, you'll need to create a custom class that extends LinearLayout and initialize in there all its properties. Something like:

public class MyLinearLayout extends LinearLayout {

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyLinearLayout(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.id.R.layout.child_container, this);
        // setup all your Views from here with calls to getViewById(...);
    }

}

Also, since your custom LieanrLayout extends from LinearLayout you can optimize the xml by replacing the root <LinearLayout> element with <merge>. Here is a short documentation and an SO link. So the for loop becomes:

for (int i = 0; i < someNumber; i++) {
    LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or any other layout params that suit your needs
    LinearLayout childContainer = new MyLinearLayout(this);
    parentLayout.addView(childContainer, params); // feel free to add or not the LayoutParams object
}
Community
  • 1
  • 1
gunar
  • 14,660
  • 7
  • 56
  • 87
  • Yes this should work. If your child container layout is particularly complicated (lots of sub view / options) then you can always make a class extending LinearLayout that sets your child_container xml with setContentView() and instantiate that class as seen here. – Andrew G Aug 30 '13 at 15:40
  • @gunar but what about my the actual content of the childContainer. I need it to contain the content from the xml. – user2426316 Aug 30 '13 at 15:43
  • hmmm ... missed that part! :D let me think – gunar Aug 30 '13 at 15:44
  • Just as Andrew said: you'll need to make a custom class that extends from LinearLayout and inflates from the xml. I'll update the answer shortly. – gunar Aug 30 '13 at 15:45