material-components-android: [Snackbar] Setting LayoutParams for SnackbarLayout doesn't work
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)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 18
- Comments: 23 (5 by maintainers)
Commits related to this issue
- Update Snackbar to respect left/right system window insets PiperOrigin-RevId: 272667571 (cherry picked from commit 0c8c6f6249839a19165209c0634da143c862cb37) — committed to material-components/material-components-android by dsn5ft 5 years ago
- [Snackbar] bugfix: setting layoutParams for snackbarLayout Solve issue:[Setting LayoutParams for SnackbarLayout doesn't work bug] https://github.com/material-components/material-components-android/is... — committed to cchencool/material-components-android by cchencool 4 years ago
- Merge pull request #1 from cchencool/cchencool-patch-snackbarLayoutParams [Snackbar] bugfix: setting layoutParams for snackbarLayout `Open #1076` — committed to cchencool/material-components-android by cchencool 4 years ago
- [Snackbar] bugfix: setting layoutParams for snackbarLayout `Open #1076` Solve issue:[Setting LayoutParams for SnackbarLayout doesn't work bug] https://github.com/material-components/material-compone... — committed to cchencool/material-components-android by cchencool 4 years ago
Here is a way to achieve this using styles (for example, just a custom bottom margin):
I temporally solved this problem by code below. Reflact field
originalMarginsinBaseTransientBottomBar, and set the bottom margin into this field. When it callBaseTransientBottomBar#updateMargins(), the real layout will be updated. It works fine on both 1.1.0 & 1.2.0 release version.Edited: Can confirm this issue. The last version where the margin behavior was working properly was with version
1.1.0-alpha101.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.
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
And then from Activity set that Drawable
MainActivity.java
Result
@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">The issue should be fixed in 1.6.0 alpha03 already. Please let me know if this is still happening. : )
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?