I'm thinking of a way to implement Android Snackbars in my app. Basically, I want to be able to show Snackbar from anywhere in the app.
As I found out, android.support.design.widget.Snackbar
performs the best when put in android.support.design.widget.CoordinatorLayout
. Otherwise, I cannot swipe it away, it shows over navigation drawer and doesn't interact with Floating Action Button.
So the question is: Is it a good practice to wrap ALL my layouts in CoordinatorLayout
, get the reference for it in a BaseActivity, so that it can be passed to Snackbar from almost anywhere?
That seems to be a solid way to ensure the Snackbar and other layout components behave correctly, but... well, means touching all layouts and having one BaseActivity which is extended by all other Activities and which would be accessed from any Fragment wanting to show a Snackbar.
Is there a better way?
The previously existing onNestedScroll(CoordinatorLayout, V, View, int, int, int, int, int) has been deprecated in favor of the new onNestedScroll(CoordinatorLayout, V, View, int, int, int, int, int, int[]) and Behavior implementations should be updated accordingly.
CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.
You can try gravity to align inside the CoordinatorLayout. android:layout_gravity="end" This worked for me.
I have implemented same like open Snackbar
from anywhere in application.
I have created one common method and just pass context and string message i need to display and no need to pass any view. Check out my code snippet.
public static void showSnackBar(Activity context, String msg) { Snackbar.make(context.getWindow().getDecorView().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG).show(); }
Using this, You will have your Snackbar
on bottom every time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With