Unresolved reference: inflate, Android studio veiwBinding error - android-studio

I was trying to implement viewBinding in Kotlin android studio project but it pops up an error like this: Unresolved reference: inflate I tried all the suggestions given on this site and the actions suggested by android studio (to import various things), but I was still not able to resolve it. My code is as follows:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View.inflate
import androidx.core.content.res.ColorStateListInflaterCompat.inflate
import androidx.core.graphics.drawable.DrawableCompat.inflate
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=ActivityMainBinding.inflate(layoutInflater)
}
}
I have already implemented veiwbinding in buid gradle:
android {
...
buildFeatures {
viewBinding true
}
...
}
Can anyone please tell me:
what's the solution to this problem?
what exactly is causing this problem?

Try adding a button in your xml and call it in the activity file by setOnClickListener. This is to check if the layout is inflating without any issue. NOTE: add setContentView(binding.root) like I did in the onCreate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=ActivityMainBinding.inflate(layoutInflater)
// Add this line
setContentView(binding.root)
// continueBtn --> the nameof the button id the xml
binding.continueBtn.setOnClickListener {
println("HELLO THERE")
}
}

Related

how to create class of findViewById?

I am a beginner ,https://i.stack.imgur.com/k8z9T.jpgin the android studio whenever I try to use findViewById.It shows an error and they ask me to create its variable but I don't know how to create it . Please tell me, I am stuck here.
You just created a function outside of your MainActivity. You have to create it inside of your activity. According to your screenshot, you just try to create a Top-level Function not a Function because it's outside of your activity. When you need to create a Function you have to create that inside your activity. Keep your eyes on Curley Brackets {}.
See the explanation to understand.
In your screenshot
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
} /* Your activity end's on here*/
private fun addNickName(view: View) {
// Your instance
// val editText = findViewById<EditText>(R.id.nickName_edit)
}
So the answer is
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
private fun addNickName(view: View) {
// Your instance
// val editText = findViewById<EditText>(R.id.nickName_edit)
}
} /* Your activity end's on here*/
Hope you understand!. Thank you

Accessing a public class (Kotlin)

I am new to kotlin and android studio's (which I am using), so this is very simple, but I ran into this problem while working on a tutorial
The issue is very simple:
I have two kotlin classes (MainActivity and why). Why contains a function test that I want to call in MainActivity.
How do I do this?
In the tutorial I simply call it like so in MainActivity
why.test()
(full code below)
But when I try to run doing this I get the error:
"Unresolved reference: test on line 13" (where I call test).
Why is this happening? How do I get this to work?
Code:
MainActivity class:
package com.example.tester
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)
work()
}
private fun work() {
why.test()
}
}
test function in why class: (test does nothing in this example)
package com.example.tester
class why {
fun test() {
var i = 0;
}
}
I think "test" should be static (or create object of why), like so:
package com.example.tester
class why {
companion object {
fun test() {
var i = 0;
}
}
}
See What is the equivalent of Java static methods in Kotlin?

Can one reference id directly in kotlin?My small program can't seem to run

Hi im new in android and i read that i can directly reference id from activity_main to mainAcctivity.kt without using findviewbyid.When i do that i get an error but my ids are correct:
This is the error Unresolved reference: button and Unresolved reference: textView
This is my code
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get reference to button
//val btnClickMe = findViewById(R.id.button) as Button
//val myTextView = findViewById(R.id.textView) as TextView
var timesClicked = 0
// set on-click listener
button.setOnClickListener {
timesClicked += 1
textView.text = timesClicked.toString()
Toast.makeText(this#MainActivity, "Hello Don.", Toast.LENGTH_SHORT).show()
}
}
}```
Please help out,i tried to import android.widget.button and textView that option is not available but when i hardcode the text appears inactive.
You can make use of ViewBinding to reduce the use of findViewById. You can find an example here.
You can go through this blog to have more insight on how to use ViewBinding.
For the above code, you can resolve the issue using the code below:
val myTextView = findViewById<TextView>(R.id.textView)
val btnClickMe = findViewById<Button>(R.id.button)

Address elements by ID instead of findViewById

I've just started learning android development and Android Studio is a complete mess after xcode ;)
I have an element with ID "mybutton" and i want to use it in my kotlin code. So, to avoid using findViewById i want to use it's id (which is "mybutton"). But when i enter mybutton in the code IDE doesn't understand it and doesn't ask me to import anything.
I've just seem in online course that it should ask if i want to use "mybutton" from activity_main but in my case it does not. So i don't know how to address the elements by id ;)
Please help
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get reference to button
val btn_click_me = findViewById(R.id.mybutton) as Button
var myTextView = findViewById(R.id.textView) as TextView
var timesClicked = 0
// set on-click listener
mybutton.setOnClickListener { // <<------ here, mybutton is red and i can't use it
Toast.makeText(this#MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
myTextView.text = "Hey it's me again, "
timesClicked += 1
}
}
Am i doing something wrong? May be i should activate something in IDE?;)
i found that i should add the following line
import kotlinx.android.synthetic.main.content_main.*
but to add it i had to add also these 2 lines
In project-level build.gradle
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
and apply the kotlin-android-extensions plugin:
In module-level build.gradle
apply plugin: 'kotlin-android-extensions'
Is there something wrong with me? It's my first "hello world" on android.. it is normal i should invent a hyperloop to use ID-reference?

Cant find reference for button, kotlin library

enter image description hereI just have this:
package com.example.world
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener() {
// Do some work here
}
}
}
But on this line:
button.setOnClickListener() {
// Do some work here
}
it says:
Unresolved reference: button
And I dont see the hint for important the correct library.
So what kind of library I have to import then?
Thank you
if I do it like this:
val btn_click = findViewById(R.id.button) as Button
btn_click.setOnClickListener(){
Toast.makeText(this#MainActivity, "YOu clicked on me", Toast.LENGTH_LONG).show()
}
Then it works.
But that is not necessarily I have read.
You dont have to make a referenct to the button.
So you just can do this:
button.setOnClickListener... but what is the correct library for that?
You can't use 'abstract' button, you need to find it in layout by element id (You can have many buttons in your layout, how you will find correct button without id?) If you wouldn't like to find layout elements by id, you can take a look on Android View Binding here: https://developer.android.com/topic/libraries/view-binding.

Resources