How to show an Android Snackbar message

To show an Android Snackbar message from an Activity or Fragment, use Java code like this:

Snackbar.make(view, "going to: " + url, Snackbar.LENGTH_LONG).show();

One key is to remember to call the show() method after make(). I have a tendency to forget to call show() and then wonder why my Snackbar message isn’t showing up. So maybe a better way to show that code is like this:

Snackbar.make(view, "going to: " + url, Snackbar.LENGTH_LONG)
        .show();

Another important point is to figure out what the correct view component should be. That code works when I call it on my WebView in my current Android app, but I’ve tried other techniques like these that I’ve found online:

View view = inflater.inflate(R.layout.activity_main, container, false);
View view = mView.findViewById(R.id.linearLayout);
View view = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
View rootView = mView.getRootView();

I only try those techniques when my Snackbar message appears to be attached to the wrong component, or at the wrong Z-level.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.