Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
            }
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Iterate them:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading