2

My code:

public class Test extends Activity {

private TableLayout tableLayout = null;
private Button add = null;

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    setContentView(R.layout.main);

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

    add = (Button)findViewById(R.id.agregar);
    add.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            TableRow fila = new TableRow(Test.this);
            Button eliminar = new Button(Test.this);
            eliminar.setText("delete");
            eliminar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            fila.addView(eliminar,new TableLayout.LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT));
            tableLayout.addView(fila,new TableLayout.LayoutParams(
                      LayoutParams.MATCH_PARENT,
                      LayoutParams.WRAP_CONTENT));
        }
    });
}

I want it to add a new row every time I click on the add Button. My xml is the following one:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <Button
                android:id="@+id/button1"
                android:text="Delete" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/agregar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tableLayout"
        android:text="Add" />
</RelativeLayout>

Every time I click on the Button, it does nothing. Can somebody help me with this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zapotec
  • 2,628
  • 4
  • 31
  • 53
  • 2
    If you found the solution to your problem please add an answer to your question and accept it so the question becomes answered. – user May 06 '12 at 07:58
  • how to add buttons inside tablerow using FOR LOOP and then for every button there will be another TABLEROW that will hold three linear layout horizontal orientation can someone please help me ? – nida Nov 19 '13 at 12:11

1 Answers1

0

(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

I found out that they can not be LayoutParams. If they are part of a TableRow, they have to be TableRow.LayoutParams and if they are inside the TableLayout, they have to be TableLayout.LayoutParams. Like the following one:

eliminar.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            fila.addView(eliminar,new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            tableLayout.addView(fila,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129