Home > other >  Kotlin cant access TextView by the ID
Kotlin cant access TextView by the ID

Time:10-02

I just started learning App development with Android Studio and Kotlin, but I have a problem. In the tutorial, it is possible to access a textView from the main XML file in the main kotlin file by just writing the id of the textView. But it is not working for me. The id does not get recognised in the kotlin file, but in the video it does. I already checked if the id is correct and I have followed every step in the video.

Example:

myTextView.text = "text of myTextView"

Thanks for help in advance!

CodePudding user response:

It requires kotlin-android-extensions but it is deprecated. So recent Android Studio may not add the required plugin to your build.gradle. You still can use it by manually add apply plugin: 'kotlin-android-extensions' to build.gradle (In addition, import is required in your kt file but the IDE will automatically do it) but it is not recommended. Instead use view binding, as explained in this official docs: https://developer.android.com/topic/libraries/view-binding

  • Related