Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 511
the_dani Avatar asked Jul 18 '18 16:07

the_dani


People also ask

What is snackbar explain it with example?

Android Snackbar is an interesting component introduced by Material Design. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them. https://material.io/components/snackbars.

What snackbar methods must be called to create a snackbar with a button?

In the onClickListener a Snackbar is created and is called. So whenever the button is clicked, the onClickListener creates a snackbar and calls it and the user sees the message. This snackbar contains an action and if clicked will show a toast.

What is a snackbar design?

Snackbars inform users of a process that an app has performed or will perform. They appear temporarily, towards the bottom of the screen. They shouldn't interrupt the user experience, and they don't require user input to disappear.


1 Answers

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();
}
like image 51
adityakamble49 Avatar answered Oct 04 '22 01:10

adityakamble49