1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help with importing id names

Discussion in 'Android Development' started by Motorhomer14, Jun 15, 2021.

  1. Motorhomer14

    Motorhomer14 Lurker
    Thread Starter

    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 (Text):
    2. val btn_click_me = findViewById(R.id.button) as Button
    4. btn_click_me.setOnClickListener {
    5.      Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
    6. }
    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 (Text):
    2. button.setOnClickListener {
    3.     Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
    4. }
    Within my activity_main.xml file I have the button id as button

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

    1. Download the Forums for Android™ app!



Share This Page