[SOLVED] How can I set tablerow of differnt width in Tablelayout?

Issue

This Content is from Stack Overflow. Question asked by Saurabh Jadhav

My expected Output

This is referenced Image of what I want,
But im unable to do it, currently im using tablelayout, what i should do?
Ofcourse my total requirement are vertical and horizontal scrolling.

My current code is below with this I cant even have row of diff width, same width get applied to all below rows of column

/* create cell element - button */
        for (int i = 0; i < 14; i++) {

            if (i == 0) {
                TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
                            100,
                        100);
//                tableRowParams.column=1;
//                tableRowParams.span=2;
                TableRow tableRow = new TableRow(this);
                View v = LayoutInflater.from(this).inflate(R.layout.item_header, tableRow, false);
                tableRow.setLayoutParams(tableRowParams);
                TextView tv_label = (TextView)v.findViewById(R.id.tv_label);
                LinearLayout ll_row = (LinearLayout)v.findViewById(R.id.ll_row);
                tv_label.setText("dynamic " + i);
                ll_row.setBackgroundColor(Color.parseColor("#6ab04c"));
//                ll_row.getLayoutParams().height = 100;
//                ll_row.getLayoutParams().width = 400;
//                ll_row.requestLayout();
                tableRow.addView(v);
                tableLayout.addView(tableRow);
            } else {
                TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
//                tableRowParams.column=1;
//                tableRowParams.span=1;

                TableRow tableRow = new TableRow(this);
                View v = LayoutInflater.from(this).inflate(R.layout.item_header, tableRow, false);
                tableRow.setLayoutParams(tableRowParams);
                TextView tv_label = (TextView)v.findViewById(R.id.tv_label);
                LinearLayout ll_row = (LinearLayout)v.findViewById(R.id.ll_row);
                tv_label.setText("dynamic " + i);
                ll_row.setBackgroundColor(Color.parseColor("#6ab04c"));
//                ll_row.getLayoutParams().height = 300;
//                ll_row.getLayoutParams().width = 200;
//                ll_row.requestLayout();
                tableRow.addView(v);
                tableLayout.addView(tableRow);
            }


        }

My current Output is this:
My Current Output



Solution

The answer is you cannot control the widths of individual cells on a row, the core concept of the TableView layout is that ALL cells in a column have the same width, thus you cannot individually adjust the width of a cell on a row.

BUT you can specify that a cell can span multiple columns with android:layout_span and this should get you what you want to do.

e.g. on Row 2 in your picture you would set the cell in the "12" column to span two columns (i.e. "12" and "13")

As an example this question gives more details


This Question was asked in StackOverflow by Saurabh Jadhav and Answered by Andrew It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?