udacity / andfun-kotlin-color-my-views

Other
30 stars 49 forks source link

issue in setListeners #8

Closed mordeLash closed 4 years ago

mordeLash commented 4 years ago

android studio doesn't recognize box_one_text, box_two_text etc. and to me it make sense that it doesn't because there is no findViewById am i missing somthing?

kristijanbiro commented 4 years ago

android studio doesn't recognize box_one_text, box_two_text etc. and to me it make sense that it doesn't because there is no findViewById am i missing somthing?

They are using an plugin that was not mentioned in the tutorial. So you need to go tobuild.gradle (Module: ColorMyViews.app) and add the id 'kotlin-android-extensions' line to the plugins:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

and after that you need to sync gradle. After the sync add import kotlinx.android.synthetic.main.activity_main.* to the imports in the MainActivity.kt file.

mordeLash commented 4 years ago

ok thankyou

aryamansriram commented 3 years ago

Hi can someone tell me why the above solution worked? Like it worked for me too but I don't exactly know why

kristijanbiro commented 3 years ago

Hi can someone tell me why the above solution worked? Like it worked for me too but I don't exactly know why

So what the plugin basically does is that it executes an findViewById on the strings that are typed. It looks at your resource file to deliver the right id.

So you can write

listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text, constraint_layout, red_button, green_button, yellow_button)

And the plugin converts it to

listOf( findViewById(R.id.box_one_text), findViewById(R.id.box_two_text), ...)

schapira commented 2 years ago

Hi can someone tell me why the above solution worked? Like it worked for me too but I don't exactly know why

So what the plugin basically does is that it executes an findViewById on the strings that are typed. It looks at your resource file to deliver the right id.

So you can write

listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text, constraint_layout, red_button, green_button, yellow_button)

And the plugin converts it to

listOf( findViewById(R.id.box_one_text), findViewById(R.id.box_two_text), ...)

thanks for the explanation .