Search code examples
android-studioandroid-layoutkotlinandroid-activitytextview

Android Studio ActivityMain.kt not finding IDs of TextView, Buttons etc from activity-main.xml


I'm trying to learn programming. Using Android Studio I have created an TextView and and Button in activity_main.xml and now I'm trining to use this two in MainActivity.kt but Android Studio cannot find the IDs of my Textview nor of my button. Am I doing something wrong?

Thanks for your help and suggestions.

OS ManjaroLinux Android Studio v. 4.1.1 installed by Flatpack using Kotlin as programming language

Images of my problem

Button declaration activity_main.xml

TextView declaration activity_main.xml

MainActivity.kt error


Solution

  • Refer ids of components using R.id.*. For example, if the id is defined by :

    android:id="@+id/buttonTxt"
    

    Then refer it by :

    val button = findViewById<Button>(R.id.buttonTxt)
    

    The same applies for any resources like strings (R.string.*), colors, dimens etc.