Convert Function Call to Lambda (SAM)

Viewed 2660

In an android program, I have the following code:

clockCheckBox.setOnClickListener((object: View.OnClickListener {
    override fun onClick(view: View): Unit {
        if (clockCheckBox.isChecked())
            enableClocks()
        else
            disableClocks()
    }
}))

In Android Studio, a tooltip comes up that says,

This inspection reports an anonymous object literal implementing a java interface with single abstract method that can be converted into call with lambda expression.

I have tried to do this, but all I get is syntax errors. Can you show me the correct syntax? I should perhaps explain that this code is in the onCreate method of my activity and clockCheckBox is a local variable defined as

val clockCheckBox = findViewById(R.id.clockCheckBox) as CheckBox
3 Answers
Related