Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to wrap all layouts in CoordinatorLayout?

Tags:

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?

like image 845
Marcel Bro Avatar asked Apr 08 '16 17:04

Marcel Bro


People also ask

Is coordinator layout deprecated?

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.

What is CoordinatorLayout used for?

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.

How do you align items in coordinator layout?

You can try gravity to align inside the CoordinatorLayout. android:layout_gravity="end" This worked for me.


1 Answers

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.

like image 190
Samir Bhatt Avatar answered Oct 26 '22 14:10

Samir Bhatt