• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help with importing id names

I am following a course where the instructor has import the GUI component id's without having to declare them. The original long version of declaring the id into variables is shown below.

Code:
val btn_click_me = findViewById(R.id.button) as Button 
 
btn_click_me.setOnClickListener {
     Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}

The short version replaces Val with button id declared within the bain listener line and then imported. When the instructor clicks on the button he gets the option to import this id into the code but I only get the option to declare it as a variable.

Code:
button.setOnClickListener {
    Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}

Within my activity_main.xml file I have the button id as button
android:id="@+id/button"

wonder if this is something I am doing wrong or a feature I have not turned on
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones