question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Snackbar] Setting LayoutParams for SnackbarLayout doesn't work

See original GitHub issue

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:

expected

Actual behavior:

actual

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:closed
  • Created 4 years ago
  • Reactions:18
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

19reactions
ricknoutcommented, Mar 31, 2020

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>
3reactions
jairrabcommented, Mar 5, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found