Sandra
Sandra

Reputation: 4259

android add layout in code

I have a layout created for displaying items, and I want to add this layout later in code. Kind of like when u have a layout created for list item, but my problem is that I don't need a list. At first I have just one item, and then if the user adds sth, I want to display another item. Can someone help me do this? Thx

Upvotes: 2

Views: 5485

Answers (1)

Aditya
Aditya

Reputation: 389

check this code snippet. This can give you some idea.

    // this is Layout to which you want to add 
   LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
   // this is what u want to add. I defined this in a layout file my_field.xml 
   View  itemView = LayoutInflater.from(context).inflate(R.layout.my_field,null, false);
   // add it to Layout 
   myLayout.addView(itemView);

Upvotes: 3

Related Questions