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



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Help importing names
  1. puppykickr
    Replies:
    4
    Views:
    241
  2. Vladdelia012
    Replies:
    3
    Views:
    250
  3. Armczbt
    Replies:
    0
    Views:
    237
  4. Armczbt
    Replies:
    0
    Views:
    187
  5. Cheesecaketiny
    Replies:
    3
    Views:
    316
  6. walkwalk
    Replies:
    4
    Views:
    321
  7. melani78
    Replies:
    2
    Views:
    230
  8. Abadi0
    Replies:
    6
    Views:
    385
  9. izaiahxxkyocerarise
    Replies:
    7
    Views:
    539
  10. rompecabezas2356
    Replies:
    18
    Views:
    686

Share This Page

Loading...