'MainActivity.kt doesn't recognise button by ID

I'm struggling with very common problem, I think. I've created a button in xml file and tagged it with ID. Then I wanted to make onClickListener in MainActivity.kt. But when I'm typing button's ID, it's marked red and it seems like Android Studio doesn't recognise it. I've tried cleaning and rebuilding project, but the problem still exist. Invalidate Caches/Restart didn't help as well. Here's XML Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor"
    android:fadingEdge="vertical"
    android:fadingEdgeLength="80dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity"
    tools:viewBindingIgnore="true">

 <Button
        android:id="@+id/btnDateBicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:backgroundTint="#3D446C"
        android:text="Select Date"
        android:textSize="25sp"
        android:textStyle="bold" />
</LinearLayout>

And here's kotlin code

package com.example.ageinminutes

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val mybtn = findViewById<btnDateBicker>()
    }
} 


Solution 1:[1]

Try this:

val mybtn = findViewById<Button>(R.id.btnDateBicker)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1