Group Group Group Group Group Group Group Group Group

setOnClickListener Q

#1

Hello,
Someone can explain me what does it mean the -> view in :
myButton.setOnClickListener { -> view

}
Can we delete it ? Or what’s the utility for it ?
Kinds regards

#2

It would actually need to be

myButton.setOnClickListener { view ->
    // some action here
}

Keeping in mind that this is a lambda (between the curly braces), this is a shorthand for a function definition. The “view ->” part is how you name the function arguments, and then you can reference the “view” argument inside the lambda. You can optionally omit the view -> section depending on what you’re doing, but it’s generally helpful to include to make it easier to read.

https://kotlinlang.org/docs/reference/lambdas.html#lambda-expression-syntax