How do I programmatically add a tableRow to TableLayout in Xamarin
android, xamarin
Solution
You need to use TableRow.Layoutparams for the buttons.. Try this code.
TableLayout _table = (TableLayout) findViewById(R.id.table);
LayoutParams layoutParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
TableRow tableRow = new TableRow(this);
Button _button = new Button(this);
_button.setText(">>");
_button.setLayoutParams(layoutParams);
tableRow.addView(_button, 0);
_table.addView(tableRow, 0);
Problem
So I have my layout that contains a TableLayout defined in my layout here ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/defaultBackground_vert" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mainLayout"> <TableLayout android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/table"> </TableLayout> </LinearLayout> ``` And I am accessing it in my codebehind and trying to add a button to a table row and add that tablerow to the table: ``` private TableLayout _table private Button _button . . . _table = FindViewById<TableLayout>(Resource.Id.table); _button = new Button(this){Text = "<"}; _button = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent); var tableRow = new TableRow(this); tableRow.AddView(_button, 0); _table.AddView(tableRow, 0); ``` The problem is that the tableRow does not show up when I run my application.