1

I have a TableLayout:

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >

<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

    <TextView
         android:id="@+id/textView2"
        android:text="Day High"
        android:textStyle="bold" >
     </TextView>

    <TextView
        android:id="@+id/textView3"
         android:gravity="center_horizontal"
        android:text="28°F" >
     </TextView>

     <TextView
        android:id="@+id/textView4"
         android:gravity="center_horizontal"
        android:text="26°F" >
     </TextView>    

</TableRow>
</TableLayout>

And a Button:

<Button
android:id="@+id/addItemTableRow"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="31dp"
android:textColor="@android:color/white"
android:text="Add row" />

I do not get to make by clicking on the button adds a new row in the table.

Any thoughts how could I implement this task?

mmBs
  • 8,421
  • 6
  • 38
  • 46
user2887315
  • 11
  • 1
  • 3

1 Answers1

1

Try this on your onCreate method of your Activity class:

  //define a onClickListener for your button
    Button btnAddItem = (Button) findViewById(R.id.addItemTableRow);
    btnAddItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                            //create a new row to add
            TableRow row = new TableRow(YourActivity.this);
                            //add Layouts to your new row
            TextView txt = new TextView(YourActivity.this);
            txt.setText("New Row"); 
            row.addView(txt);
            //add your new row to the TableLayout:
            TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
            table.addView(row);

        }
    });
Diego
  • 738
  • 1
  • 7
  • 19