Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add margins to Snackbar view

Tags:

I'm updating my current app to use snackbars, in the Google spec they show various ways of using them http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-specs

Example A:

Snackbar with margin

Example B:

Snackbar pinned to layout

Here's my code atm:

Snackbar snackbar = Snackbar.make(mParentLayout, displayMessage,          Snackbar.LENGTH_LONG);     snackbar.setAction(actionMessage, mClickListener);     snackbar.show(); 

I get the result in Example B,

How can i add margins?

like image 293
AndroidEnthusiast Avatar asked Jul 20 '15 10:07

AndroidEnthusiast


People also ask

How do I adjust my snackbar height?

setTextSize(24); //increase max lines of text in snackbar. default is 2. sbNoAct. show(); int height = sbView.

How do you give a snackbar shape in flutter?

First, you have to create a SnackBar which can be done by calling the following constructor. The only required parameter is content which is the widget to be displayed inside the snackbar. In most cases, the widget is a Text widget, but you can also use other types of Flutter widget.

How do I use snackbar?

This example demonstrates how do I use snackBar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

In addition to Saeid's answer, you can get the default SnackBar layout params and modify them as you want:

public static void displaySnackBarWithBottomMargin(Snackbar snackbar, int sideMargin, int marginBottom) {     final View snackBarView = snackbar.getView();     final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();      params.setMargins(params.leftMargin + sideMargin,                 params.topMargin,                 params.rightMargin + sideMargin,                 params.bottomMargin + marginBottom);      snackBarView.setLayoutParams(params);     snackbar.show(); } 
like image 68
BamsBamx Avatar answered Oct 21 '22 12:10

BamsBamx


None of the above solutions worked for me.

But, I got one trick that we can use with the help of translation.

Snackbar snackbar = Snackbar.make(mActivity.getWindow().getDecorView().getRootView(), message, Snackbar.LENGTH_SHORT); View snackBarView = snackbar.getView(); snackBarView.setTranslationY(-(convertDpToPixel(48, mActivity))); snackbar.show(); 

you can find convertDpToPixel method here

like image 32
M.Usman Avatar answered Oct 21 '22 12:10

M.Usman