dobriy denyok
dobriy denyok

Reputation: 47

How to use with to multiple params Kotlin?

i have 4 views and i want to change their height and width to the same value, how can i reduce the amount of code here?

private fun changeSize() {
        with(binding) {
            with(leftBotIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(leftTopIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(rightBotIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(rightTopIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
        }
    }

Upvotes: 0

Views: 56

Answers (1)

Tenfour04
Tenfour04

Reputation: 93609

Iterate them:

private fun changeSize() = with(binding) {
    arrayOf(leftBotIv, leftTopIv, rightBotIv, rightTopIv)
        .forEach {
            it.layoutParams.height = height
            it.layoutParams.width = width
        }
}

Upvotes: 1

Related Questions