Anko: How to use lparams

Created on 27 May 2017  路  2Comments  路  Source: Kotlin/anko

Hi,

I am playing with anko for a few days and like it pretty much so far. Created some custom views which also went quite well.

The question I have is when to use lparams at which place? I understand that at top level elements you should do something like:

verticalLayout {
    lparams(...) { ... }
}

But sometimes you should / can use:

verticalLayout {
    editText {
        lparams(...) {...} // <---- seems to work sometimes as well
    }.lparams(...) {...} // <---- is this preferred / different from within editText{} ?
}

What is the difference between these two approaches? I can provide an example where it is actually working differently / not applying the lparams if required.

Thanks for the info already.

Anko Layouts question

Most helpful comment

You should always write

verticalLayout {
    // code
}.lparams { ... }

The other ways may be bug-prone. For example, by writing this:

verticalLayout {
    lparams { ... }
}

you actually say "I want to put the newly created verticalLayout into another LinearLayout". If you put it to some other layout, you'll get a runtime exception.

All 2 comments

You should always write

verticalLayout {
    // code
}.lparams { ... }

The other ways may be bug-prone. For example, by writing this:

verticalLayout {
    lparams { ... }
}

you actually say "I want to put the newly created verticalLayout into another LinearLayout". If you put it to some other layout, you'll get a runtime exception.

Alright, thanks for the heads up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bapspatil picture bapspatil  路  4Comments

evansherry picture evansherry  路  4Comments

SUPERCILEX picture SUPERCILEX  路  4Comments

telenc picture telenc  路  3Comments

lupajz picture lupajz  路  3Comments