Jayakrishnan
Jayakrishnan

Reputation: 4667

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

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

Upvotes: 8

Views: 2462

Answers (2)

ice1000
ice1000

Reputation: 6569

Simply

shadowView.layoutParams.width = 200

Upvotes: 12

92AlanC
92AlanC

Reputation: 1387

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

Upvotes: 3

Related Questions