How to create a full screen TableLayout programmatically (table height collapses unintendedly)

android, tablelayout

Solution

Try this..

LinearLayout.LayoutParams tableParams = 
                  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
                          LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams rowParams = 
                  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
        LinearLayout.LayoutParams itemParams = 
                  new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                          LinearLayout.LayoutParams.MATCH_PARENT, 1f);

                        LinearLayout tableLayout = new LinearLayout(context);
                tableLayout.setLayoutParams(tableParams);
                tableLayout.setOrientation(LinearLayout.VERTICAL);
                tableLayout.setBackgroundColor(0xff7434a6);

                for (int row = 0; row < 2; row++) {
                    LinearLayout tableRow = new LinearLayout(context);
                    tableRow.setOrientation(LinearLayout.HORIZONTAL);
                    tableRow.setLayoutParams(rowParams);

                    for (int column = 0; column < 2; column++) {
                        Random color = new Random();
                        int randomColor = 
                          Color.argb(255, color.nextInt(256), 
                                          color.nextInt(256),
                                          color.nextInt(256));

                        TextView textView = new TextView(context);
                        textView.setLayoutParams(itemParams);
                        textView.setBackgroundColor(randomColor);

                        tableRow.addView(textView);
                    }

                    tableLayout.addView(tableRow);
                }

OR In your rowParams change `TableRow` to `TableLayout` and try.

TableLayout.LayoutParams tableParams = 
                  new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
                                               TableLayout.LayoutParams.MATCH_PARENT, 1f);
        TableLayout.LayoutParams rowParams = 
                  new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
                TableRow.LayoutParams itemParams = 
                  new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 
                                            LayoutParams.FILL_PARENT, 1f);

                TableLayout tableLayout = new TableLayout(MainActivity.this);
                tableLayout.setLayoutParams(tableParams);
                tableLayout.setBackgroundColor(0xff7434a6);

                for (int row = 0; row < 2; row++) {
                    TableRow tableRow = new TableRow(MainActivity.this);
                    tableRow.setLayoutParams(rowParams);

                    for (int column = 0; column < 2; column++) {
                        Random color = new Random();
                        int randomColor = 
                          Color.argb(255, color.nextInt(256), 
                                          color.nextInt(256),
                                          color.nextInt(256));

                        TextView textView = new TextView(MainActivity.this);
                        textView.setLayoutParams(itemParams);
                        textView.setBackgroundColor(randomColor);

                        tableRow.addView(textView);
                    }

                    tableLayout.addView(tableRow);
                }

Problem

The following `TableLayout` matches exactly my requirements (it fills its parent and columns are stretched evenly): ``` <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#7434a6" > <TableRow android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#836492" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#103456" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#958453" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#756aa9" /> </TableRow> </TableLayout> ``` However, as I want to have the number of rows being configurable I started to create my layout programmatically: ``` public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Context context = getActivity(); TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT); TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f); TableRow.LayoutParams itemParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f); TableLayout tableLayout = new TableLayout(context); tableLayout.setLayoutParams(tableParams); tableLayout.setBackgroundColor(0xff7434a6); for (int row = 0; row < 2; row++) { TableRow tableRow = new TableRow(context); tableRow.setLayoutParams(rowParams); for (int column = 0; column < 2; column++) { Random color = new Random(); int randomColor = Color.argb(255, color.nextInt(256), color.nextInt(256), color.nextInt(256)); TextView textView = new TextView(context); textView.setLayoutParams(itemParams); textView.setBackgroundColor(randomColor); tableRow.addView(textView); } tableLayout.addView(tableRow); } return tableLayout; } ``` Up to a certain point this works not bad as well. Only issue I have is, that my table collapses vertically: What is wrong here? Thought my code is doing the same as my XML. I searched and tried quite a lot but didn't get the right idea.

Original source