Dynamically adding views to RelativeLayout inside ScrollView

Go To StackoverFlow.com

8

I am trying to add dynamically created several RelativeLayouts into a LinearLayout which is inside a RelativeLayout, which is inside a ScrollView. When the total height of the all views exceed the size of the phone screen, all views are displayed correctly. But when the total size of dynamically added views is not enough for filling the screen, only the first RelativeLayout element is shown and the others are not displayed in the screen. I am really hopeless and do not understand why.

Here is the code to dynamically populate views inside linear layout:

LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);

LayoutInflater inflater = (LayoutInflater)
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(Comment c: commentsList) {

    RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(
        R.layout.list_item_comment, null, false);

        TextView tv = (TextView) layoutItem.findViewById(R.id.textView);
        ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);

        // set tv's text
        // set iv's image and onclicklistener, nothing fancy here, everything goes well

        commentsLayout.addView(layoutItem);
}

Here is list_item_comment.xml:

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>
    <ImageView 
    android:id="@+id/imageView"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    />

    <TextView 
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:textSize="16sp"
    android:layout_toRightOf="@id/imageView"
    />
</RelativeLayout>

And here is the xml file for this activity:

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout"
>
...

    <ScrollView
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:id="@+id/scrollView"
    >

        <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/relativeContainer"
        >

        ...

            <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/comments_layout"
            />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

And the screenshots:

Without sufficient layouts: (INCORRECT, needs to show 3 comments)

Incorrect one

With enough layouts: (CORRECT ONE, screen is filled)

correct one

I just need to show all three comments in the first case :/ Thanks in advance.

2012-04-03 23:57
by ecem
Why not use a ListView with a header and a footer - miguel_ibero 2012-04-04 17:05
actually normally I have a lot of other views above the ones that look like a list: several imageviews, buttons etc. so this is not a common case but I need to solve this situation too, because I cannot rely on the fact that every other view in this activity will be initialized. any thoughts - ecem 2012-04-29 15:33


0

instead of fill_parent, try changing the layout_height of the <RelativeLayout> of your list_item_comment.xml to wrap_content.

Also, why do you need another <RelativeLayout> inside your <ScrollView> of the xml of your activity. The LinearLayout is sufficient to do what you want your activity to look like. Maybe you can just remove it.

2012-04-04 17:01
by yojoannn
I tried your suggestion but it does not seem to solve the problem :/ Actually this is not a common case of my activity, normally there must be one 2 imageviews, one button, several textviews etc therefore relativelayout makes my job easier. But this is a case too (although it is rare), therefore I should find a solution : - ecem 2012-04-11 13:53
@ecem, Were you able to find a solution for this - WindsurferOak 2013-09-29 16:33
Ads