Home > database >  Android studio kotlin, cannot directly select ID without findViewById()
Android studio kotlin, cannot directly select ID without findViewById()

Time:09-26

I'm new to android studio and I'm leaning it from this video

At 50:45 he says that we can directly use the id without findViewById (), for him it shows just import that Id, but when I tried the same I'm not getting what he is getting. Can anyone tell me what's wrong, is that feature removed in the latest update??

CodePudding user response:

"kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById" So, this feature`s been deprecated almost a year ago. The new recommended way of working with view tree is View Binding.

Or you could write some lazy extension function that uses findViewById under the hood.

CodePudding user response:

The way you're talking about is Kotline Extensions for view, for which you apply it in the build.gradle file using apply plugin: 'kotlin-android-extensions', sync the project as prompted by the Android studio and it starts working.

But, Kotlin extensions has also been deprecated for view binding which is null-safe, also easy to implement and better.

To read about view binding, read the Android documentation on ViewBinding here and about migrating here. You can also go with this article to help with setting up Android view binding.

CodePudding user response:

You can use the Kotlin Android Extensions check here or with the help of data binding or view binding you can just use the id directly in to your java activity class

  • Related