Reputation: 1561
I am starting to learn android development using Kotlin. I am trying to call object ID in my MainActivity.kt but I am getting an error Unresolved reference : btnDatePicker
Given below is the code
<Button
android:id="@+id/btnDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#0E0B0B"
android:text="SELECT DATE"
android:textColor="#C5BBBB"
android:textSize="20sp"
android:textStyle="bold"
app:backgroundTint="#186C64" />
Given below is the code in MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnDatePicker.setOnClickListener { Toast.makeText(this, "works", Toast.LENGTH_LONG).show() }
}
}
On hovering over btnDatePicker I see the below options but not sure how to proceed
Upvotes: 0
Views: 799
Reputation: 152827
Looks like you're trying to use synthetic view binding. Your project needs to have the kotlin-android-extension
plugin enabled in build.gradle like:
apply plugin: 'kotlin-android-extensions'
After that you should be able to import your btnDatePicker
.
Note that kotlin-android-extensions and synthetic view binding is deprecated so you are probably better off with Jetpack view binding. See https://developer.android.com/topic/libraries/view-binding/migration
Upvotes: 1