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:
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?