0

I have a class:

public final class MySnackbar<T>
    extends BaseTransientBottomBar<AccountSnackbar<T>>

I try to add margins to MySnackbar by calling in the ctor to:

  private void setSnackbarContainerMargins(Context context, View content) {
    MarginLayoutParams layoutParams = (MarginLayoutParams) getView().getLayoutParams();
    int horizontalMargin =

    layoutParams.leftMargin = horizontalMargin;
    layoutParams.rightMargin = horizontalMargin;
    layoutParams.bottomMargin =

    parent.setLayoutParams(layoutParams);
  }

At runtime, I see the margins stay the same.

Is there any other way to add extra margins to the my custom snackbar?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Check this [answer](https://stackoverflow.com/questions/32425191/style-snackbar-in-theme-app/58607153#58607153). You can do it with a custom style but there are some limits. – Gabriele Mariotti May 16 '20 at 18:02
  • Why don't these work? https://stackoverflow.com/a/44295639/311130 or https://stackoverflow.com/a/33969220/311130 – Elad Benda May 16 '20 at 20:54

1 Answers1

0

You can try this, where CoordinatorLayout is your parent layout for snackBar:

int sideMargin = 12, marginBottom = 12;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    sideMargin = 24;
    marginBottom = 24;
}
MySnackbar snackbar = Snackbar.make(findViewById(R.id.addCoordinatorLayout), R.string.notification, Snackbar.LENGTH_SHORT);
final View snackBarView = snackbar.getView();
final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();
    
params.setMargins(params.leftMargin + sideMargin,
                  params.topMargin,
                  params.rightMargin + sideMargin,
                  params.bottomMargin + marginBottom);
    
snackBarView.setLayoutParams(params);
snackBarView.setBackground(getDrawable(R.drawable.round_corner));
snackbar.show();