Search code examples
androidmaterial-designmaterial-components-androidandroid-snackbarsnackbar

How to create a Snackbar like in the material documentation?


I've seen this new Snackbar Style in the Outlook for Android app and now looked for it in the Material Documentation:

https://material.io/design/components/snackbars.html

Does anybody know how to create those "offset Snackbars"?


Solution

  • how to create those "offset Snackbars"?

    You can get the LayoutParams of SnackBar and add Bottom and Side Margin to it

    Try out like code below

    public static void showSnackbar(Snackbar snackbar, int sideMargin, int marginBottom) {
        final View snackBarView = snackbar.getView();
        // Depend upon your parent Layout Use `LayoutParams` of that Layout
        final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) snackBarView.getLayoutParams();
    
        params.setMargins(params.leftMargin + sideMargin,
                    params.topMargin,
                    params.rightMargin + sideMargin,
                    params.bottomMargin + marginBottom);
    
        snackBarView.setLayoutParams(params);
        snackbar.show();
    }