2

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

3
  • Are you going to display each item the same way? Commented May 8, 2012 at 15:09
  • 1
    Is there a reason you don't want to use a listview? (I'm assuming each item that you're adding is the same?). Maybe if you give us more information on what you're looking for as an end result we can provide some suggestions. Commented May 8, 2012 at 15:26
  • Yes, I am going to display each item the same way, and I need a layout for each. (due to having textview, imageview etc.) The reason I am not using a listview is that I don't want the items that I am adding to be scrollable. They are part of another layout, which I want to add a scrolling option. Commented May 9, 2012 at 8:05

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, and the layout is not added. I have to mention that myLayout.add(itemView) did not work, so I changed it in myLayout.addView(itemView), but it didn't do anything. Maybe you can't add a layout as a view? :S
That's not a problem u can add Layout as View. I am already doing the same. Here "R.layout.my_field" actually is LinearLayout which has more fields in it.
Ok, so I guess sth else is the problem. In my case, I have a LinearLayout (vertical) in wich I have TextView's, ImageView's etc. and to that layout I want to add another, at the bottom (maybe a couple of times if needed, because I am using the layout as an item). I will see and try sth out, and hopefully solve the problem. Thx anyway:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.