Interactive Notifications in Android: A Snackbar Tutorial

Explore the versatile world of Android Snackbar widgets in our blog post. From interactive notifications to customizing appearance, learn everything in our tutorial. Enhance your app user experience and take your message display to a new level!

Introduction

In this tutorial, we will discuss various forms of Android Snackbar widgets and implement them in our application.

Android Snackbar

Snackbar in Android is a new widget introduced with the Material Design library, serving as a replacement for Toast. Snackbar is a lightweight widget used to display messages at the bottom of the screen, with swipe dismissal enabled. A Snackbar can optionally include an action button.

Difference between Toast and Snackbar

  1. Toast messages can be customized and displayed anywhere on the screen, while a Snackbar can only be displayed at the bottom of the screen.
  2. A Toast message does not have an action button, but a Snackbar can optionally have an action button. However, a Snackbar should not have more than one action button.
  3. A Toast message cannot be dismissed until the time expires, while a Snackbar can be swiped away before the time expires.

Sample Code

To display a basic Snackbar, we use the following code:

Snackbar snackbar = Snackbar.make(coordinatorLayout, "www.journaldev.com", Snackbar.LENGTH_LONG);
snackbar.show();

Here, coordinatorLayout is the root view of the activity, "www.journaldev.com" is the message to be displayed in the Snackbar, and Snackbar.LENGTH_LONG indicates the duration of display.

Project Structure

The project structure remains unchanged, with activity_main.xml containing the CoordinatorLayout and content_main.xml containing three buttons for different Snackbar types.

Sample Code for Action Callback Snackbar

The action button is used to trigger an action when clicked. Here is a sample code for the action callback Snackbar:

two.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar snackbar = Snackbar
                .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
                .setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
                        snackbar1.show();
                    }
                });

        snackbar.show();
    }
});

Sample Code for Custom Snackbar

Custom Snackbar allows us to customize the appearance of the Snackbar. Here is a sample code for a custom Snackbar:

three.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar snackbar = Snackbar
                .make(coordinatorLayout, "Try again!", Snackbar.LENGTH_LONG)
                .setAction("RETRY", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                });
        snackbar.setActionTextColor(Color.RED);
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();
    }
});

Conclusion

Snackbar is a useful widget in Android that provides an enhanced user experience. By implementing different Snackbar types, we can display messages in an interactive and appealing manner.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: