7

Regardless of whether or not it is a good idea to create a custom Snackbar, I have a custom Snackbar and I can't seem to get rid of the margins. I've tried several things such as adjusting in code and in layout as shown below. Nothing seems to work.

I am using the approach laid out by Yakiv here:

final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();
params.setMargins(0,0,0,0);

and other approaches such as

android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp" ...

The Snackbar: The snackbar with margins

My layout file:

<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="android.support.design.internal.SnackbarContentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/realBlack"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp">

<TextView
    android:id="@+id/snackbar_text"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:paddingTop="@dimen/design_snackbar_padding_vertical"
    android:paddingBottom="@dimen/design_snackbar_padding_vertical"
    android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
    android:paddingRight="@dimen/design_snackbar_padding_horizontal"
    android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
    android:maxLines="@integer/design_snackbar_text_max_lines"
    android:layout_gravity="center_vertical|left|start"
    android:ellipsize="end"
    android:textAlignment="viewStart"
    android:text="get some"/>

<Button
    android:id="@+id/snackbar_action"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
    android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
    android:layout_gravity="center_vertical|right|end"
    android:minWidth="48dp"
    android:visibility="gone"
    android:textColor="?attr/colorAccent"
    style="?attr/borderlessButtonStyle"/>
</view>

The calling code:

        _firmwareSnackbar = CustomSnackbar.make((ViewGroup) _rootView, Snackbar.LENGTH_INDEFINITE);

        _firmwareSnackbar.setText(_flipper.getContext().getString(R.string.settings_firmware_available));

        _firmwareSnackbar.setAction(_flipper.getContext().getString(R.string.settings_firmware_start), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _firmwareSnackbar.dismiss();
                _firmwareSnackbar = null;

            }
        });

        _firmwareSnackbar.show();
Community
  • 1
  • 1
Rob Allen
  • 2,871
  • 2
  • 30
  • 48

5 Answers5

7

If you are using material components, then add this code to your style.xml file under values

 <style name="Widget.SnackBar" parent="Widget.MaterialComponents.Snackbar">
    <item name="android:layout_margin">0dp</item>
 </style>

then

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="snackbarStyle">@style/Widget.SnackBar</item>
</style>

This will change margin for all existing snackBar in the app. So if you want to show a gap around SnackBar you can use padding.

        val snackBar = Snackbar.make(homeLayout, "", Snackbar.LENGTH_INDEFINITE)
        val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout
        snackBarLayout.setPadding(8, 0, 8, 0)
Harsha Bhadra
  • 223
  • 2
  • 5
  • 13
5

In dimens.xml.

Use this:

 <dimen name="design_snackbar_padding_horizontal">0dp</dimen>

But remember that this will get applied to all the snackbars in your application.

shubhamgarg1
  • 1,619
  • 1
  • 15
  • 20
4

Before showing the Snackbar you can remove the parent layout padding in the following way:

//...
_firmwareSnackbar.getView().setPadding(0,0,0,0);
_firmwareSnackbar.show();
Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
0

Add to theme of App or Activity <item name="snackbarStyle">@style/Widget.Design.Snackbar</item>

Ринат
  • 96
  • 1
  • 2
0

You shoud remove paddings from a parent view:

View snackBarLayout = findViewById(R.id.mainLayout);
Snackbar globalSnackbar = Snackbar.make(snackBarLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) globalSnackbar.getView();  

layout.setPadding(0,0,0,0); 
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52