52.9 Adding an Action to the Snackbar
An action may also be added to the Snackbar which performs a task when tapped by the user. Edit the MainActivity.kt file and modify the Snackbar creation code to add an action titled “My Action” configured with an onClickListener named actionOnClickListener which, in turn, displays a toast message:
binding.fab.setOnClickListener { view ->
displayMessage("FAB Clicked")
Snackbar.make(view, "Action complete", Snackbar.LENGTH_LONG)
.setAction("My Action", actionOnClickListener).show()
}
Within the MainActivity.kt file add the listener handler:
.
.
import android.view.View
.
.
var actionOnClickListener: View.OnClickListener = View.OnClickListener { view ->
displayMessage("Action clicked")
Snackbar.make(view...