Programmatically add Views inside a (Linear)Layout (that is inside a ScrollView)

12,836

I am not very sure how you inflate your comments, but here is how I would do it.

Declare the comments Layout as a LinearLayout and give it an id, and then get a reference to that LinearLayout :

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

and then just add child Views to this layout :

commentsLayout.addView(newComment);
Share:
12,836
Dimmy3
Author by

Dimmy3

Updated on June 04, 2022

Comments

  • Dimmy3
    Dimmy3 almost 2 years

    I have an app that, after some clicking, shows activity with news contents. I want at a bottom of that to show comments, which are dynamically loaded in async task.

    One approach is to use ListView and custom ArrayAdapter, but, i would have to put ListView inside a ScrollView, and it is is a problem, even if i manually override ListView's height. It shows one list item, and one part of the following one.

    The other approach is to define LinearLayout as a holder for comments, than inflate another LinearLayouts defined in it's own xml file, populate it's contents, attach to holder's view. It works fine, from program's point of view, except it pushes comments below contents of the news. It's like, it creates another scrollable view of comments underneath contents od news (contents overlap it).

    Is there any other way of doing this that has nothing to do with lists, or how can i make the other approach to work.

    The relevant code snippet from xml layout that holds news contents is:

    <LinearLayout> //global layout
        <LinearLayout>
            //views that hold title, date, content
        </LinearLayout>      
    
            <LinearLayout 
               android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/commentsHolder"
                android:orientation="vertical">
    
            <View 
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="#ed1b24"/>
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="KOMENTARI"
                android:layout_marginLeft="5dp"/>
            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#ed1b24"/>
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:orientation="vertical"
                android:id="@+id/comments_holder_view"
                android:layout_weight="1">
            </LinearLayout>
    
    
        </LinearLayout>
    
    </LinearLayout>
    

    and code that corresponds to one comment is:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/comments_holder_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">
    
    <TextView 
        android:id="@+id/comment_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/comments_back_details"
        android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView 
            android:id="@+id/comment_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/categoryStyle"
            />
    
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" | "
            style="@style/categoryStyle"
            />
    
        <TextView 
            android:id="@+id/comment_pubdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/categoryStyle"/>
    
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        android:background="#d1d1d1">
    </LinearLayout>
    

    Thank you very much for any reply. This is quite important for me.