45.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.java 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(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("My Action", actionOnClickListener).show();
}
});
Within the MainActivity.java file add the listener handler:
View.OnClickListener actionOnClickListener = new View.OnClickListener() {
...