Search code examples
androidkotlinanko

Anko - Setting row and column value for an element in gridLayout


How do I set column and row value for an element in gridLayout in Anko?

I tried several versions, but this does not compile:

   return UI {
        gridLayout() {
            columnCount = 2
            rowCount = 3
            button("1x1") {

            }.lparams() { column = 1; row = 1 }
        }
    }.view

When I put it like this (as a function), or as properties inside curly brackets, it says it cannot reference column or row. When I provide them as arguments to lparams, it says none of the following functions can be called with arguments supplied.


Solution

  • As stated in the GridLayout documentation:

    Children occupy one or more contiguous cells, as defined by their rowSpec and columnSpec layout parameters. Each spec defines the set of rows or columns that are to be occupied; and how children should be aligned within the resulting group of cells.

    You can use rowSpec and columnSpec like so:

    return UI {
        gridLayout() {
            columnCount = 2
            rowCount = 3
            button("1x1") {
    
            }.lparams {
                rowSpec = GridLayout.spec(1)
                columnSpec = GridLayout.spec(1)
            }
        }
    }.view