Less than 100 pages covering Kotlin syntax and features in straight and to the point explanation.

Listening to a button clicks is a common task in application development. A listener is set to listen and act upon click events. OnClickListener is an interface used to listen to click events of a button or any other components of type View .

View & Click Listener

View is the basic building block for UI components.

Views can register or set listeners that will be notified when an event happens to the view.

Components of type View can set click listener that get notified on click event.

can set click listener that get notified on click event. setOnClickListener(OnClickListener l) is used to set the click listener.

is used to set the click listener. Click listener is an instance of type OnClickListener .

View.java

public void setOnClickListener ( OnClickListener l ) { .... }

OnClickListener is an interface with a single abstract method “SAM”.

public interface OnClickListener { void onClick ( View v ); }

So, we need to pass an instance of type OnClickListener to setOnClickListener() function.

We can achieve this in the following ways.

( 1 ) The simplest old way, implement OnClickListener interface

The simplest way to create an instance of type OnClickListener is by implementing it.

is by implementing it. Our activity can implement the interface OnClickListener and override onClick() .

// 1. extend View.OnClickListener class MainActivity : AppCompatActivity (), View . OnClickListener { private lateinit var textView : TextView override fun onCreate ( savedInstanceState : Bundle ?) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) val button = findViewById < Button >( R . id . button ) textView = findViewById < TextView >( R . id . textView ) // 2. pass this as instance of OnClickListener button . setOnClickListener ( this ) } // 3. override onClick() override fun onClick ( v : View ?) { textView . setText ( "Clicked!" ) } }

( 2 ) Use object expression

Use object expression to create an instance from an anonymous class implementing OnClickListener .

class MainActivity : AppCompatActivity () { private lateinit var textView : TextView override fun onCreate ( savedInstanceState : Bundle ?) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) val button = findViewById < Button >( R . id . button ) textView = findViewById < TextView >( R . id . textView ) // create an instance from anonymous class button . setOnClickListener { object : View . OnClickListener { override fun onClick ( v : View ?) { textView . setText ( "Clicked!" ) } } } } }

( 3 ) Lambda

Since OnClickListener is an interface with a single abstract function, we can use lambda.

is an interface with a single abstract function, we can use lambda. The onClick() function takes a parameter of type View and returns void or Unit for Kotlin.

function takes a parameter of type and returns or Unit for Kotlin. So, the function type of the lambda should be (View) -> Unit

class MainActivity : AppCompatActivity () { private lateinit var textView : TextView override fun onCreate ( savedInstanceState : Bundle ?) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) val button = findViewById < Button >( R . id . button ) textView = findViewById < TextView >( R . id . textView ) // create an instance from anonymous class button . setOnClickListener { v -> textView . setText ( "Clicked!" ) } } }