By Alvin Alexander. Last updated: August 7, 2018
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.