Unresolved reference for objects from activity_main.xml in kotlin (Android Studio)

Viewed 1356

I'm following a kotlin / android tutorial and encountered a problem where I'm not able to reference anything by the id. Here in this example Android Studio gives me "Unresolved reference for btnDatePicker. I found this post on SO this where the solution suggests to add the line import kotlinx.android.synthetic.main.activity_main.*. Unfortunately I'm getting "unresolved reference" for kotlinx.

Data: Android Studio 4.1.3 Build #Al-201.8743.12.41.7199119, built on March 11, 2021

Here is my code. I highlighted the line with the unresolved reference with //??

MainActivity:

package com.example.simpleageinminutescalc

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.content_main.* //??

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnDatePicker.setOnClickListener{ Toast.makeText(this,"Button works!",Toast.LENGTH_SHORT).show()}
    }
}

activity_main.xml file:

<?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"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@color/backgroundColor"
android:padding="16dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="CALCULATE YOUR"
    android:textColor="@color/textColor"
    android:textSize="20sp"
    android:textStyle="bold"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="AGE"
    android:textColor="@color/ageTextColor"
    android:background="@color/ageBackgroundColor"
    android:textSize="20sp"
    android:textStyle="bold"
    android:padding="10dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="IN MINUTES"
    android:textColor="@color/textColor"
    android:textSize="20sp"
    android:textStyle="bold"/>

<Button
    android:id="@+id/btnDatePicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:textColor="@color/fadedColor"
    android:textStyle="bold"
    android:text="Select Date"
    android:textSize="20sp"
    app:backgroundTint="#FFFFFF"/>

<TextView
    android:id="@+id/tvSelectedDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20sp"
    android:textColor="#844046"
    android:textSize="18sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="Selected Date"
    android:textColor="@color/fadedColor"
    android:textSize="22sp"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/tvSelectedDateInMinutes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20sp"
    android:textColor="#844046"
    android:textSize="35sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="Age in minutes"
    android:textColor="@color/fadedColor"
    android:textSize="22sp"
    android:textStyle="bold"/>
</LinearLayout>
0 Answers
Related