I want to bind "this" context to listener in kotlin
Asked
Active
Viewed 95 times
-3
-
1Exactly the exact same way as in Java – EpicPandaForce Oct 15 '18 at 12:07
2 Answers
0
In kotlin class you have to write the activity name with @
annotation in order to get the context
of the class.
button.setOnClickListener {
val intent = Intent(this@MainActivity, PicturesActivity::class.java)
startActivity(intent)
}
or if you have override the onclick
method just register the onclick
method inside onCreate
as
buttonX.onSetOnclickListener(this@ActivityName)

Raza
- 791
- 7
- 22
-
-
when ever you want to get the context of class by `this` in kotlin you have to write the name of the class attached with `this@`.. like `this@MainActivity` – Raza Oct 15 '18 at 12:22
-1
Here is an example on how to use the onClickListener in Kotlin
buttonX.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
//Code here ....
})

Sandeep
- 455
- 4
- 26