0

I have 3 relative layouts and i need to create a single setcontetntview which is a combination of these layouts each added 3 times and sharing equal screen space and it needs to be done programatically. I started by creating a new layout adding just 2 screens,

    RelativeLayout primaryLayout = new RelativeLayout(this);
    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    RelativeLayout newLayout = (RelativeLayout)layoutInflater.inflate(R.layout.layout3, null, false);
    RelativeLayout newLayout1 = (RelativeLayout)layoutInflater.inflate(R.layout.layout4, null, false);
    primaryLayout.addView(newLayout);
    primaryLayout.addView(newLayout1);
    setContentView(primaryLayout);

This is displaying only the layout4. Also, if i add same layout again, its giving error stating the specified child already has a parent, you must call removeview() on the child parent first. Please help!

bharath
  • 953
  • 4
  • 17
  • 30

2 Answers2

0

You must use addRule to the layout param to set the views with respect to each other. As for the complaint about re-adding a view, you can try two independent instances of the view. While it will be the same layout, the parent won't know that.

EDIT:

To make occupy half screen you have to tell the parent view how much space to allot for the child view:

RelativeLayout.LayoutParams forChild = new RelativeLayout.LayoutParams(30,40);
childView.setLayoutParams(forChild);
learner
  • 11,490
  • 26
  • 97
  • 169
  • Thanks for the reply, i added rules, but how to make them occupy half screen each?? – bharath Mar 15 '13 at 18:02
  • I have edited to show you how to set size of child view. You don't have to use actually numbers, you can use things like `wrap_content`, etc. Just play around with it. – learner Mar 15 '13 at 20:16
0

THe reason its displaying only layout4 is that the primaryLayout is a relative layout. Unless you specify something telling it where they go in that layout, it all goes in the upper left corner. So everything is being put on top of one another.

You can't add the same layout multiple times. You'd have to reinflate it once for every version you want, and add the results. Its like OOP- the RelativeLayout is an instance of the layout, inflating instantiates a new one.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127