3

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>

<code>TableLayout</code> fills parent

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:

enter image description here

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.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • just try to set static height to itemParams Params. and tell me what happen? – M D Jan 08 '14 at 10:09
  • @M D: setting a static height and width will be applied correctly (tested this already before). However, I wanted to get around calculating the right height in order to match the parent container. – Trinimon Jan 08 '14 at 10:26
  • @Trinimon have you tried my ans.. – Hariharan Jan 08 '14 at 10:45

1 Answers1

5

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);
                }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • This is definitely a solution in order to get the desired layout. However, it answers only 50% of my question 'cause it should work with a `TableLayout` as well, shouldn't it? Question is how? Anyway +1 so far :) – Trinimon Jan 08 '14 at 10:48
  • @Trinimon No prob dude. – Hariharan Jan 08 '14 at 11:17
  • Thanks for your update, Tamilan: I made some experiments with your code and saw that only one(!) change is essential to get my initial code running as intended: the `rowParams` have to be `TableLayout.LayoutParams` and not `TableRow.LayoutParams`. No need for the table weight, no need to replace `MATCH_PARENT` or `0`. Thanks a lot! You rescued me from getting crazy! ;) – Trinimon Jan 08 '14 at 12:35
  • Small adjutment: `itemParams` has to be `new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1f);` - otherwise cells won't be even once you fill items with more complex content. This has not been covered by the solution/comments/question. – Trinimon Jan 09 '14 at 09:46