[Snackbar] Setting LayoutParams for SnackbarLayout doesn't work
See original GitHub issueDescription:
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)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:18
- Comments:22 (5 by maintainers)
Top Results From Across the Web
Cannot set margin in snackbar - android - Stack Overflow
I'm trying to set margins in all the 4 sides of the snackbar layout. Coordinator Layout: CoordinatorLayout.LayoutParams layoutParams = ( ...
Read more >Snackbar - Android Developers
Snackbar s should still work with AppCompat themes, which don't specify a snackbarButtonStyle . This method helps to check if a valid ...
Read more >How To Add A Margin Or Padding To A Snackbar - ADocLib
Assuming you are working with CoordinatorLayout you can change the settings of the snack bar layout before calling show.
Read more >design/src/android/support/design/widget/Snackbar.java
Snackbars can contain an action which is set via ... mView = (SnackbarLayout) inflater.inflate(R.layout.layout_snackbar, ... LayoutParams lp = mView.
Read more >Snackbars - Material Design
LENGTH_SHORT (Show the snackbar for a short period of time). Note: Snackbars work best if they are displayed inside of a CoordinatorLayout ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is a way to achieve this using styles (for example, just a custom bottom margin):
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.