How can I add a row to a table in Android Studio?

127 views Asked by At

I'm trying to add a row to a table programmatically. I have already created two rows manually in the xml file.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TableLayout table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        table = findViewById(R.id.table);
        TableRow newRow = new TableRow(this);
        table.addView(newRow);
    }
}

activitiy_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
<RelativeLayout
...
<TableLayout
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_edittext_table"
    android:padding="20dp">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="20dp">

        <EditText style="@style/EditTextStyle" />
        <EditText style="@style/EditTextStyle" />
        <EditText style="@style/EditTextStyle" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="20dp">

        <EditText style="@style/EditTextStyle" />
        <EditText style="@style/EditTextStyle" />
        <EditText style="@style/EditTextStyle" />
    </TableRow>

</TableLayout>
...
</RelativeLayout>
</ScrollView>

But instead of a new row, I get a new TableLayout:
enter image description here enter image description here

0

There are 0 answers