0

I want to add rows to my table already in xml.

Here is my JAVA code

TableLayout tl=(TableLayout)findViewById(R.id.table1);
        //TableRow tbrow = new TableRow(this);
        TableRow tbrow= (TableRow)findViewById(R.id.row);
        ((TextView)tbrow.findViewById(R.id.title)).setText("hiiiiiifg");
        ((TextView)tbrow.findViewById(R.id.author)).setText("okhjksdbfk");
        tl.addView(tbrow);
        tl.requestLayout();

Myxml code

<LinearLayout
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:weightSum="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.56">

    <TableLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.88"
        android:paddingTop="1dp"
        android:id="@+id/table1"
        android:background="@color/colorAccent">


        <TableRow android:id="@+id/row">

            <TextView
                android:background="@drawable/cell_shape"

                android:text=" Title"
                android:layout_column="0"
                android:layout_weight="1"
                android:textSize="18sp"
                android:textColor="@android:color/black"

                android:id="@+id/title"
                android:maxWidth="30dp"
                />
            <TextView
                android:background="@drawable/cell_shape"

                android:text=" Author"
                android:layout_column="0"
                android:layout_weight="1"
                android:textSize="18sp"
                android:textColor="@android:color/black"
                android:maxWidth="30dp"
                android:id="@+id/author" />
            <TextView
                android:background="@drawable/cell_shape"

                android:text=" Year"
                android:layout_column="0"
                android:layout_weight="1"
                android:textSize="18sp"
                android:textColor="@android:color/black"
                android:maxWidth="30dp"
                android:id="@+id/year" />
            <TextView
                android:background="@drawable/cell_shape"


                android:text=" Pages"
                android:layout_column="1"
                android:layout_weight="1"
                android:textSize="18sp"
                android:textColor="@android:color/black"
                android:maxWidth="30dp"
                android:id="@+id/pages" />
            <TextView
                android:background="@drawable/cell_shape"

                android:text=" Keywords"
                android:layout_column="2"
                android:layout_weight="1"
                android:textSize="18sp"
                android:textColor="@android:color/black"
                />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b_add"
        android:layout_marginBottom="16dp"
        android:background="@android:drawable/ic_input_add"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:minWidth="60dp"
        android:layout_gravity="right"
        android:onClick="OnClick"
        />

</LinearLayout>

What I want to do is add rows from my java class. The code I posted crash, so if you have suggestions or already know how to deal with this kind of situation I would like your advice.

Nas
  • 2,158
  • 1
  • 21
  • 38
  • You can't add `View`s that are already in your layout. You need to create new ones. The linked duplicate at the top explains this, and gives an example of how to do everything in code. A possibly easier way would be to define your `TableRow` layout in a separate XML file, and inflate and modify that as needed. An example of that is shown in [this post](http://stackoverflow.com/a/8132212). – Mike M. Dec 18 '16 at 09:58
  • I just need to add a new layout where i will define the TableRow. One last question, do I need to change the manifest file ? –  Dec 18 '16 at 10:25
  • Nope. The manifest is where you register overall app components, like `Activity` and `Service` classes. It doesn't need to know anything about the layouts. – Mike M. Dec 18 '16 at 10:27

0 Answers0