Description:
There is no effect when trying to set LayoutParams for the SnackbarLayout (i.e. adding some margins).
I tried to add margins to the Snackbar because I wanted it not to stick to the bottom edges of the screen. Instead, I wanted it to float just like shown in the Material design guidelines.
This bug does not occur on Library version 1.0.0. I've reproduced this issue with version 1.1.0 and also with the latest alpha (1.2.0-alpha05).
Expected behavior:

Actual behavior:

Source code:
Snackbar snackbar = Snackbar.make(getView(), "Test", Snackbar.LENGTH_LONG);
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbar.getView();
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
snackbarLayout.getLayoutParams();
layoutParams.setMargins(32, 0, 32, 32);
snackbarLayout.setLayoutParams(layoutParams);
snackbar.show();
Android API version: 29
Material Library version: 1.1.0 and 1.2.0-alpha05
Device: Samsung Galaxy S10 and Android Emulator (Pixel 2 API 29)
Edited:
Can confirm this issue. The last version where the margin behavior was working properly was with version 1.1.0-alpha10 1.1.0-beta01. Margin Issue first started showing up on 1.1.0-beta02.
Scanning through the commits on 1.1.0-beta02, this commit on 75e20b78fadf1f89d43ee71d9365e12d4d8f8213 seems to make changes to margin calculations changes.
Guys, I am just faced this issue right before release the app to Google Play. Fortunately before, and not after...
Facing same issue could anyone help!!
I believe you are using the wrong style to setup the Snackbar, you need to use this in the app theme:
- @style/Widget.MaterialComponents.Snackbar
Instead of @style/Widget.MaterialComponents.Snackbar.FullWidth.
But I believe there is something weird happening there.
I tried setting the paddings or margins on the Snackbar like:
<item name="snackbarStyle">@style/Snackbar</item>
<item name="snackbarTextViewStyle">@style/Snackbar.TextView</item>
<style name="Snackbar" parent="@style/Widget.MaterialComponents.Snackbar.FullWidth">
<item name="backgroundTint">@color/lh_slate_opacity_90</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:paddingStart">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingBottom">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginRight">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
<style name="Snackbar.TextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
<item name="android:fontFamily">@font/poppins</item>
<item name="android:textSize">12sp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:paddingStart">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingBottom">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginRight">0dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
Get the same behaviour for:
com.google.android.material:material:1.2.0-alpha05 com.google.android.material:material:1.1.0But this only changes the left paddings:

it seems there is a general issue here, setting margins dont seem to work via xml.
None of these work:
<style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
<item name="android:layout_marginLeft">50dp</item>
<item name="android:layout_marginRight">50dp</item>
<item name="android:layout_marginBottom">100dp</item>
</style>
especially the bottom one is very unfortunate. Just moving up the Snackbar is sth we are doing via code (which also doenst work anymore)
Reason and the only one that works seems:
<item name="android:layout_margin">100dp</item>
but this will then mess up side margins too.
Here is a way to achieve this using styles (for example, just a custom bottom margin):
<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="snackbarStyle">@style/Widget.App.Snackbar</item>
</style>
<style name="Widget.App.Snackbar" parent="Widget.MaterialComponents.Snackbar">
<item name="android:layout_margin">@null</item>
<!-- Use default Snackbar margins for top/left/right -->
<item name="android:layout_marginTop">@dimen/mtrl_snackbar_margin</item>
<item name="android:layout_marginLeft">@dimen/mtrl_snackbar_margin</item>
<item name="android:layout_marginRight">@dimen/mtrl_snackbar_margin</item>
<!-- Custom bottom margin, this could work for top/left/right too -->
<item name="android:layout_marginBottom">100dp</item>
</style>
Last post solution seems to work thanks.
But "snackbarStyle" will apply for every SnackBar in app.
What if we need to have different snackbars with different margin?
Im not happy with setting @null, how can you know that it will work fine in all devices?
is there any update on this?
Also experiencing this issue. Any update?
@sylviestephanies @ssawchenko
I resolver this issue just by adding this
<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
before i was using this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I temporally solved this problem by code below. Reflact field originalMargins in BaseTransientBottomBar, and set the bottom margin into this field. When it call BaseTransientBottomBar#updateMargins(), the real layout will be updated. It works fine on both 1.1.0 & 1.2.0 release version.
private boolean fixSnackBarMarginBottomBug(Object snackBar, int height) {
try {
Class snackbarClass = Class.forName("com.google.android.material.snackbar.Snackbar");
Field originalMarginsField = snackbarClass.getSuperclass().getDeclaredField("originalMargins");
originalMarginsField.setAccessible(true);
Rect fixedOriginalMargins = new Rect();
fixedOriginalMargins.bottom = height;
originalMarginsField.set(snackBar, fixedOriginalMargins);
return true;
} catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
LogUtils.d(TAG, "fixSnackBarMarginBottomBug error.");
}
return false;
}
Adding CoordinatorLayout or Frame Layout and then setting margin didn't work for me
To tackle this problem use Drawable Background where use item to set Margin and shape to set desired Padding
container_snackbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Set Margin Here -->
<item
android:left="20dp"
android:right="20dp"
android:bottom="10dp"
android:top="10dp">
<!-- Insert your shape here: -->
<shape android:shape="rectangle" >
<solid android:color="#FE4C4C" />
<!-- Padding for inner text-->
<padding android:left="25dp" android:right="10dp" android:bottom="10dp" android:top="10dp" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
And then from Activity set that Drawable
MainActivity.java
Snackbar snack = Snackbar
.make(activity,"Hello World 馃殌",Snackbar.LENGTH_INDEFINITE);
snack.getView()
.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.contianer_snackbar));
snack.show();
Most helpful comment
Here is a way to achieve this using styles (for example, just a custom bottom margin):