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?vhallac– vhallac2012-05-08 15:09:43 +00:00Commented May 8, 2012 at 15:09
-
1Is 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.Gophermofur– Gophermofur2012-05-08 15:26:32 +00:00Commented 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.Sandra– Sandra2012-05-09 08:05:19 +00:00Commented May 9, 2012 at 8:05
Add a comment
|
1 Answer
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);
3 Comments
Sandra
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
Aditya
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.
Sandra
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:)