Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i am unable to import anything from activity_main.xml to MainActivity.kt by its id such as textview or button

I saw I lot of videos says that the Kotlin can recognize the buttons (views) automatically in MainActivity.kt I try this but it doesn't work for me in android studio 4.1 when I'm using the usual code with :

var button_name = findViewById(R.id.buttonName)

it works fine but when I'm using the code directly like this :

buttonName.setonclicklistiner{}

the IDE doesn't recognize the button

PS : this the imports in the mainactivity

import android.os.Bundle

import android.view.View

import androidx.appcompat.app.AppCompatActivity

What can i do for access my button or textView automatically in MainActivity.kt

like image 698
kumar bittu Avatar asked Jan 26 '26 22:01

kumar bittu


2 Answers

In build.gradle(module) make sure plugins contains following 3 plugins -

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}
like image 62
Karan Choudhary Avatar answered Jan 28 '26 10:01

Karan Choudhary


Open build_gradle(:app) and make sure you have these two lines in dependencies block

dependencies {

    apply plugin : "kotlin-android"

    apply plugin : "kotlin-android-extensions"
    ...
}
like image 20
Arpita Avatar answered Jan 28 '26 10:01

Arpita