Search code examples
androidkotlinonclicklistener

setOnClickListener unresolved reference


All I want for now is to have series of buttons in my main activity, that moves me into other intents where I can test some other things and proceed with learning.

I really don't like to leave warnings behind, so when android:onClick throws Old versions of the platform do not properly support resolving android:onClick I read a lot and figured out that I should use setOnClickListener. I saw many answers about setOnClickListener, I found many solutions, yet it took me a lot of time to get to this:

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

        val introduction = findViewById<Button>(R.id.introduction)
        val needXML = findViewById<Button>(R.id.needXML)

        introduction.setOnClickListener {
            Toast.makeText(this@MainActivity, "button 1 works!", Toast.LENGTH_SHORT).show()
        }
        needXML.setOnClickListener {
            Toast.makeText(this@MainActivity, "button 2 works!", Toast.LENGTH_SHORT).show()
        }
    }
}

And still I get Unresolved reference: Button. I googled it a lot and finally managed to fix the issue by adding:

import android.widget.Button

Finding import android.widget.Toast to make simple alerts was easy, since this part was included in many tutorials, but import android.widget.Button - not at all.

So, the story ends here, and my questions are:

1: Is the way I'm using setOnClickListener even correct, or is there a better way than implementing an val for every single clickable element?

2: I'd like to use when (kotlin version of switch), to determine what was clicked... is this possible, or am I overthinking simple things?

3: I was reading a lot of tutorials/answer for the setOnClickListener, and saw many "just do buttonId.setOnClickListener" tips. And yet it was a miracle that I found import android.widget.Button - just like it was an obvious thing. It is clearly not obvious for me. How should I know in future, what should I import in order to use other functions? Perhaps I should start with reading some documentations - if this is the case, than could anyone post me a link to a pleace where I should start? I'm trying to teach everything by myself, but I'm getting there from scratch, and now I really feel like I missed something very important.


Solution

  • Is the way I'm using setOnClickListener even correct, or is there a better way than implementing an val for every single clickable element?

    findViewById<Button>(R.id.introduction).setOnClickListener {
    
    }
    

    should also work, seeing as though you don't need an actual reference to the button later.

    I'd like to use when (kotlin version of switch), to determine what was clicked... is this possible, or am I overthinking simple things?

    there's a way to do it, but it really isn't needed, it becomes a massive when statement which could be hard to manage, stick to what you're doing here.

    How should I know in future, what should I import in order to use other functions?

    experience, practice, reading documentation, and letting the IDE help you. depending on your OS, android studio can provide the option to import the necessary dependencies for you, usually you can hover your cursor over an unresolved reference (usually red) to see if android studio provides you an option to import