Home » Kotlin setonclicklistener example

Kotlin setonclicklistener example

  • by
Kotlin setonclicklistener example

1. Overview

In this article, we will see an example for Kotlin setOnClickListener. You can use setOnClickListener to register a callback to be invoked when the view is clicked. If the view is not clickable, this makes it clickable.

To learn more about other Kotlin topics, refer to these articles.

2. Kotlin setOnClickListener example

You can implement setOnClickListener for any of your android views:

2.1. Kotlin direct way example

Just like in Java, create an instance of View.OnClickListener and pass the listener to the view using setOnClickListener(View.OnClickListener)

For example, the following code creates an instance of View.OnClickListener and overrides the onClick method. This method would be called when the view is clicked. You can write the action you want to perform inside this method.

button.setOnClickListener(object : View.OnClickListener {
    override fun onClick(view: View?) {
        // your code logic goes here
    }

})

2.2. Kotlin setOnClickListener with lambda

You can replace the listener with the lambda expression to make your code more concise and readable.

We define lambda with curly braces {} that take variables as a parameter (if any) and the body of the function follows the -> operator. For example, { a, b -> a + b }

An interface with only one abstract method is called a functional interface, or a Single Abstract Method (SAM) interface.

Instead of creating a class that implements a functional interface manually, you can use a lambda expression.

With a SAM conversion, Kotlin can convert any lambda expression whose signature matches the signature of the interface’s single method into the code, which dynamically instantiates the interface implementation.

For example, you can rewrite the above sample code with lambda as:

button.setOnClickListener {
    // your code logic goes here
}

You can simply use lambda without SAM conversion by changing the onClick method as a lambda expression:

button.setOnClickListener(View.OnClickListener { view ->
    // your code logic goes here
})

2.3. OnClickListener Activity

Your activity can implement the OnclickListener interface and override the onClick method.

Inside this method, you can check the view which is being clicked as below:

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

        findViewById(R.id.button1).setOnClickListener(this)
    }
        
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.button1 ->{
                // your code logic here
            }
        }
    }
}

2.4. OnClickListener instance

You can create an onClickListener instance as a variable and pass it to your setOnClickListener as below:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById(R.id.button1).setOnClickListener(listener)
    }
    
    val listener = View.OnClickListener { view ->
        when (view.getId()) {
            R.id.button1 -> {
                // your code logic goes here
            }
        }
    }
}

3. Conclusion

To sum up, we have learned to implement setOnClickListener in Kotlin along with example.

Leave a Reply

Your email address will not be published. Required fields are marked *