Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Kotlin View width set error "Val cannot be reassigned"

Tags:

android

kotlin

In Kotlin i am getting IDE error saying "Val cannot be reassigned" when i try to set width programmatically, Please see the code written in the onCreate() of Activity class,

shadowView.width = 200

Here shadowView is a View added in the layout

like image 212
Jayakrishnan Avatar asked Apr 04 '18 02:04

Jayakrishnan


2 Answers

Simply

shadowView.layoutParams.width = 200
like image 89
ice1000 Avatar answered Nov 15 '22 08:11

ice1000


In order to set a view's width programmatically you should change its width in its layout params, like the example below:

val layoutParams = shadowView.layoutParams
layoutParams.width = 200
shadowView.layoutParams = layoutParams
like image 40
92AlanC Avatar answered Nov 15 '22 08:11

92AlanC