Tutorials  /  JavaScript

Interactive Notifications in Android: A Snackbar Tutorial

Ccentron Redaktion · September 2024 ·3 min read ·JavaScript, 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.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

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:

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:

Code
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:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren