How to set table column weights for the table layout

240 Views Asked by At

I have two table layouts. One to display title and one more to display the data. The table with data should be scrollable hence written in a separate table layout. The xml looks like this:

    <TableLayout
        android:id="@+id/title_table_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/upper_line"
        android:layout_marginEnd="@dimen/dp108"
        android:layout_marginStart="@dimen/dp60"
        android:layout_marginTop="@dimen/dp15" />

<ScrollView
        android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/dp108"
        android:layout_marginStart="@dimen/dp60"
        android:layout_marginTop="@dimen/dp15"
        app:layout_constraintTop_toBottomOf="@+id/lower_line"
        android:scrollbars="vertical"
        android:scrollbarSize="@dimen/dp4"
        android:scrollbarAlwaysDrawVerticalTrack="true">

        <TableLayout
            android:id="@+id/data_table_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/upper_line"
            />

The number of columns are fixed and the titles also are fixed. But the data is dynamic and could be of varied sizes. So I have added the weight for each row and for each column. The code looks like this

 fun addRowHeaders(){
        val tr = TableRow(context)
        tr.layoutParams = getLayoutParams()
        tr.weightSum = 7F
        tr.addView(getTextView(context?.resources?.getString(R.string.settings_type)!!,0))
        tr.addView(getTextView(context?.resources?.getString(R.string.settings_name)!!,1))
        tr.addView(getTextView(context?.resources?.getString(R.string.settings_date)!!,2))
        tr.addView(getTextView(context?.resources?.getString(R.string.settings_price)!!,3))
        tr.addView(getTextView(context?.resources?.getString(R.string.settings_payment_method)!!,4))
        binding.titleTableView.addView(tr,getTblLayoutParams())
    }

private fun getTextView(title:String, id:Int): View? {
        val tv = TextView(context)
        tv.text = title
        tv.setTextColor(resources.getColor(R.color.update_service_text_color))
        tv.textSize = resources.getDimension(R.dimen.table_text)
        tv.layoutParams = getLayoutParams(id)
        return tv
    }

 private fun getLayoutParams(id: Int): LayoutParams {
        val params = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT
        )
           if (id == 1 || id == 4)
            params.weight = 2F
        else
            params.weight = 1F
        return params
    }

I want the second and last column to be in the different sizes. With the title it is working fine. But when I am using the same code for dynamic data table it does not behave the same way, It takes the size of the longest text. The code for dynamic table is below:

 fun addTransactionData(){

        for (data in transactionList!!){
                val tr = TableRow(context)
                tr.layoutParams = getLayoutParams()
                tr.weightSum = 7F
                tr.addView(getTextView(data.type, 0))
                tr.addView(getTextView(data.title, 1))
                val date = convertDateString(data.date)
                tr.addView(getTextView(date, 2))
                tr.addView(getTextView(data.price, 3))
                tr.addView(getTextView(data.paymentMethod, 4))
                binding.dataTableView.addView(tr, getTblLayoutParams())
        }

    }

Please let me know how to make this uniform for both the table

1

There are 1 best solutions below

0
Andrew On

You cannot directly control the width of any column in a TableLayout, you can only indirectly control how the layout grows or shrinks a column.

From the documentation

The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable

The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT

Also the column width matching is done per TableLayout so there is no guarantee that your title_table_view will match your data_table_view (they might look like they align if they have similar length text in the cells)

The weightSum value is totally ignored in a TableRow

Thus TableLayout is not suitable for what you need.

I faced similar problems when trying to design a Table Layout with a fixed header column, in the end I had to design my own replacement for TableLayout with similar auto basic sizing of columns that you then have director control of the size. You might be able to implement what you want with that (See the "Bonus Feature" of https://github.com/Zardozz/FixedHeaderTableLayout )