combine 3 relative layouts multiple times Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of combine 3 relative layouts multiple times without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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!

Answer

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.

We are here to answer your question about combine 3 relative layouts multiple times - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji