1

I want to add a new row by clicking the button id-AddButton, I found many google sources but I could not do so.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <TextView
                android:id="@+id/NameTextView"
                android:layout_width="93dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20sp"
                android:layout_marginLeft="20sp"
                android:layout_marginTop="10sp"
                android:text="Name"
                android:textColor="@color/black"
                android:textSize="28sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/PersonName"
                android:layout_width="320dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20sp"
                android:layout_marginLeft="20sp"
                android:ems="10"
                android:hint="Name"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Mobile Number"
                android:layout_marginLeft="20sp"
                android:textColor="@color/black"
                android:textSize="28sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/PersonPhone"
                android:layout_width="325dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:layout_marginLeft="20sp"
                android:hint="Mobile number"
                android:inputType="phone" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Address"
                android:layout_marginLeft="20sp"
                android:textColor="@color/black"
                android:textSize="28sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/PersonAddress"
                android:layout_width="328dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20sp"
                android:ems="10"
                android:hint="Delivery Address"
                android:inputType="textPostalAddress" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20sp"
                android:text="                         Order Details"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TableLayout

                android:id="@+id/MainTable"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:stretchColumns="0,1,2"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="#000000"

                    >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_column="0"
                        android:layout_margin="1dp"
                        android:background="#FFFFFF"
                        android:gravity="center"
                        android:text=" Date "
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_column="1"
                        android:layout_margin="1dp"
                        android:background="#FFFFFF"
                        android:gravity="center"
                        android:text="Item"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_column="2"
                        android:layout_margin="1dp"
                        android:background="#FFFFFF"
                        android:gravity="center"
                        android:text="Quantity"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textStyle="bold" />
                </TableRow>
            </TableLayout>

            <Button
                android:id="@+id/AddButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Add" />

            <Button
                android:id="@+id/DoneButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Done" />

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

I need to add multiple rows dynamically when the button add is clicked, I am using Button.setOnClickListener in java but I am stuck on creating a new row. Please help me out if anyone can. One more thing, I had another account on which it is showing I have reached the limit of posting questions, can I know the limit and why it is showing so as I have only posted around 4-5 questions.

1
  • The space in " Order Details" is purposely. Commented May 8, 2021 at 9:56

1 Answer 1

1

Inside your onClickListner of the add button, implement the code to add TableRow and its element programmatically.

JAVA PART

TableLayout tableLayout;
TableLayout tableLayout = (TableLayout) findViewById(R.id.MainTable);

Create table row header for new row

TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);    
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));

Add data to the row I'll add two TextViews to the TableRow created, you can add as much as you need.

TextView label_hello = new TextView(this);
label_hello.setId(20); // define id that must be unique
label_hello.setText("HELLO"); // set the text  
label_hello.setTextColor(Color.WHITE);      
label_hello.setPadding(5, 5, 5, 5); 


TextView label_android = new TextView(this); 
label_android.setId(21);// define id that must be unique
label_android.setText("ANDROID..!!"); // set the textlabel_android.setTextColor(Color.WHITE); // set the color 
label_android.setPadding(5, 5, 5, 5); // set the padding (if required)

tr_head.addView(label_hello);// add the column to the table row 
tr_head.addView(label_android);

After adding the columns to the table row its time to add the table row the main table layout that we fetched at the start

tableLayout.addView(tr_head, new TableLayout.LayoutParams(
                     LayoutParams.FILL_PARENT,                 
                     LayoutParams.MATCH_CONTENT));      

SUGGESTION
It is recommended to use RecyclerView with a custom layout in this kind of scenario. The approach which you are implementing will cause serious performance issues if there's a number of data. Also, the way you can add/read/remove data can be made much easier using RecyclerView

Sign up to request clarification or add additional context in comments.

3 Comments

is the background colour necsessary in tr_head.setBackgroundColor(Color.GRAY); ?
Can you please explain the recycler view also?
SetBackgroundColor is not manadatory, please go through the article to get an idea of RecyclerView programmingroot.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.