I have 4 buttons and want to have 1 onClickListener
to detect which button was clicked (get the id of the button).
How can I achieve this?
button1
button2
button3
button4
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
I envision creating a when
statement like so:
when (R.id) {
R.id.button1 -> // do something
R.id.button2 -> // do something
R.id.button3 -> // do something
R.id.button4 -> // do something
else -> // do something
}
however I'm not sure how to target "any button clicked" and how the onClickListener
would look in my Activity. Any idea?