I had to add certain iterations of the same type of data to a certain View. You can do all of the adding in code, but you’re probably better off doing all the layout in a seperate place (XML), and just including multiple instances of that XML.

How to proceed: first, you’ll need an inflator (or is it inflater?) to get your View in code. In this example there is an XML with a LinearLayout as top element, and we want to add some of the TextViews that are present in another XML.

LinearLayout wrapper = (LinearLayout) findViewById(R.id.newCommentWrapper);

This View is present in the current Layout. Lets get our “to include” View:

View inflatedView = View.inflate(this, R.layout.test, null);
wrapper.addView(inflatedView);

This is all good, but keep in mind that if you want to add this multiple times you’ll have to avoid this error:

The specified child already has a parent. You must call removeView() on the child’s parent first.

You CAN just add 2 of the same Views, but you must be careful not to add the same instance twice.

View inflatedView;
inflatedView = View.inflate(this, R.layout.test, null);
wrapper.addView(inflatedView);

inflatedView = View.inflate(this, R.layout.test, null);
wrapper.addView(inflatedView2);

It’s really simple to add a single View, but we do want to change some of the stuff. No fear: you can just point to the id’s in the inflated view with findViewById! As an example: the View in the XML that is going to be “included” has a child called “comment”, that has to get a specific content (text):

inflatedView = (LinearLayout) View.inflate(this, R.layout.comment, null);
((TextView) inflatedView.findViewById(R.id.comment)).setText("you commentString goes here");

You can add the final inflatedView to your current Layout with “addview”.
Wrapping it all up, you’ll have a master layout XML, an XML with your ‘child’ that you want to add and of course some code.

MasterLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" style="@style/yourStyle1">
	<LinearLayout android:orientation="vertical" style="@style/yourStyle1" android:id="@+id/commentWrapper">
	</LinearLayout>
</ScrollView>

SingleComment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/yourStyle1" android:id="@+id/comment">
	<TextView android:id="@+id/comment" style="@style/yourStyle1"/>
</LinearLayout>

Java Snippet:

setContentView(R.layout.MasterLayout);
LinearLayout wrapper = (LinearLayout) findViewById(R.id.CommentWrapper);
LinearLayout inflatedView;

int j = nmbrOfCmnts;
for(int i=0;i < j ;i++){
	String content = getYourComment(i);
	inflatedView = (LinearLayout) View.inflate(this, R.layout.SingleComment, null);
	((TextView) inflatedView.findViewById(R.id.comment)).setText(commentUser);
	wrapper.addView(inflatedView);
}

I don’t know if this is the best method, but it let’s me keep the layout of one comment in a nice and seperate XML, while I am still able to programmatically add instances of that View to my current layout.

10 comments on Add a View to a wrapper multiple times with inflate

  1. smag says:

    hey.. m trying to do exactly same thing..
    but, only one view is visible on screen and a lot of empty space..

    the only difference is that the layout m inflating is a bit more complex..

    any thoughts?

  2. Nanne says:

    Hmm, without example it is hard to debug, but it could be dat you don’t have scrollview (you can’t scroll to the pieces that are ‘fallen off’ the screen), or that one of the things has a “fill_parent” -> if the first view you add is fill parent, it might just be that it fills it with “empty”, and there is no real room available left for the next couple of views..

    I’d start with removing all “fill parent” mentions, and then check if you see more. Re-add them to recreate your layout later?

  3. Objectlive says:

    nice exsample.

    Thanks.

  4. Maxim says:

    So good stuff man, thank you

  5. khalil_log says:

    Thanks a lot

  6. Chris says:

    this is HOT!
    Thanks so much for sharing!

  7. Sam says:

    Perfect, a powerful technique simply explained!

  8. John says:

    Everywhere else they say this can’t be done. It works great, thank you so much for posting it.
    Regards,
    John

  9. sataniccrow says:

    about Smag problems:
    check the LinearLayout which contains the items you’re going to insert and set the android:orientation to VERTICAL.

    => android:orientation=”vertical”

  10. Leo says:

    Thanks sataniccrow, that was what I was missing!