Shatranaft
Shatranaft

Reputation: 119

Programmatically add textView to LinearLayout. Items added, but seems like they are empty

I need scrollableView with image and textViews below it.

I have such layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff">

    <EditText
        android:id="@+id/comment_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/addCommentButton"
        android:inputType="text"
        android:textColor="#ff000000" />

    <Button
        android:id="@+id/addCommentButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/comment_text"
        android:text="Send" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/comment_text"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="top">

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="description"
                android:maxHeight="300dp" />

            <LinearLayout
                android:id="@+id/comments"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:orientation="vertical">
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Then i trying to add new items.

@ViewById
protected LinearLayout comments;

TextView textView = new TextView(this);
textView.setTextColor(Color.BLACK);
textView.setText(comment.getMessage());
comments.addView(textView, 0);

And i got this, but comment.getMessage() is not empty. What i'm doing wrong?

Please look at screenshots

https://i.sstatic.net/8ChWy.png https://i.sstatic.net/ZGxSQ.png

Upvotes: 2

Views: 244

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff"
    android:orientation="vertical"
    android:padding="5dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="description"
                android:adjustViewBounds="true"
                android:maxHeight="300dp" />

            <LinearLayout
                android:id="@+id/comments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:orientation="vertical">
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">
        <EditText
            android:id="@+id/comment_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:textColor="#ff000000" />

        <Button
            android:id="@+id/addCommentButton"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Send" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions