1

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.

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Zorann
  • 335
  • 2
  • 16
  • `Is the way I'm using setOnClickListener even correct,` Do you see that Toast() when the button is clicked? As you are not setting a listener. Does that even compile? – blackapps Feb 01 '22 at 12:41
  • You can use `buttonId.setOnClickListener` its not a problem . if you lots of Views and wants to handle click inside a Block like `when` or `switch` . you can let your class Implement `OnClickListener` and put a when statement with `view.getId()` inside the overridden `onClick()` . Its basic stuff i.e `Inheritance` . you might get a another warning about Id being non-final when using inside Switch i am not sure but anyway [here It is](https://stackoverflow.com/questions/7840914/android-resource-ids-suddenly-not-final-switches-broken). – ADM Feb 01 '22 at 12:43
  • @blackapps Yes, it does work. What I mean by asking if it is correct is rather the fact, if this is how it should be done. – Zorann Feb 01 '22 at 12:54
  • the IDE (Android Studio) should suggest any imports when they are missing, by hovering over anything that has a red line under it. Many other errors are often easily fixed by whatever the IDE suggests. Try for yourself. Remove that `import android.widget.Button` and hover your mouse over any red underlined `Button` references in your code. There will be a simple option to add the import. In general when you see a red underlined word and you don't know why, you should just check what the IDE has to say about it – Ivo Feb 01 '22 at 13:04

1 Answers1

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?

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

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • `usually you can hover your cursor over an unresolved reference (usually red) to see if android studio provides you an option to import` I'm discovering it still - it is great and powerfull tool. Yet in this case in place `introduction.setOnClickListener` it was suggesting that I should create a variable "introduction" since he didn't know what it was :( – Zorann Feb 01 '22 at 12:56
  • 1
    if you have the proper imports, there should be no need to declare a variable here, this code compiles fine for me – a_local_nobody Feb 01 '22 at 13:00