1

I am Creating a table made of 10X10 cells and it won't (after creating all cells) fit to its parent.

  • To make it easier to understand before I will show my code, the table looks like this:

enter image description here

As you can see, every table cell is a button with "Test" as a text, I cant make this 10X10 grid spread to fill its parent (the views exit the screen horizontally and won't fill vertically)


I am creating every cell programmatically like this:

    val table = findViewById<TableLayout>(R.id.table)
    for (i in 0..9) {    //10 rows inside the table
        val row = TableRow(this)
        for (j in 0..9) {    //10 cells inside every row

            val cell = Button(this)   
            cell.layoutParams =  TableRow.LayoutParams(

                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT
            )

            cell.text = "test"
            cell.setTextColor(Color.RED)
            cell.setBackgroundColor(Color.WHITE)
            cell.setPadding(0, 0, 0, 0)
            row.addView(cell)
        }
        table.addView(row)
    }
  • My next step was to change the way I am setting the layoutParams for every cell, I have tried to change every cell dimensions like this:

      cell.layoutParams =  TableRow.LayoutParams(
                 //table = the view that contains all cells
    
                 table.width/5,     //make the cell width = 20% of the parent width
                 table.height/10    //make the cell height= 10% of the parent height           
            ) 
    

For some reason, this made the whole table disappear and setting the layoutparams to MATCH_PARENT had the same effect as WRAP_CONTENT.


I have checked android - setting LayoutParams programmatically and LayoutParams but I haven't found the answer.

What am I doing wrong that makes my table disappear, and how can I make the cells fit to the table size?

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53

1 Answers1

1
  1. you write table.width/5 so you probably use j in 0..4

  2. See the reference for TableLayout:

    The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined.

    Use android:stretchColumns to have the table adjust the button widths correctly.

    But,

    The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively ViewGroup.LayoutParams.MATCH_PARENT and ViewGroup.LayoutParams.WRAP_CONTENT.

    This means that, by the book, you cannot fill the parent with a table vertically. But by hook or by crook, you can force the height of each button to be exactly 1/10 of the full height.

    What you can't do, is take table.height while building your layout, because at this stage the height of the parent is not known yet. I hope you can calculate this height based on the screen size.

  3. With such programmatic setup, you don't need table layout, horizontal linear views inside vertical that fills the parent could be enough.

  4. With ConstraintLayout you can specify width and height of elements as percents of the parent, see https://android.jlelse.eu/whats-new-in-constraint-layout-1-1-0-acfe30cfc7be

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 1
    About 1 - mistake while editing the question thanks. *You can probably use android:stretchColumns* , **good tip**, now the cells are stretching/shrinking inside the row (I have used `android:shrinkColumns="*"` to alow shrink). Any idea of how can I make each row stretch or shrink to make the table fit vertically as well? – Tamir Abutbul Sep 18 '19 at 20:43
  • 1
    *More from docs:* The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively ViewGroup.LayoutParams.MATCH_PARENT and ViewGroup.LayoutParams.WRAP_CONTENT. – Alex Cohn Sep 19 '19 at 05:24