Add row to a table ANDROID

89 views Asked by At

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.

0

There are 0 answers