I'm updating my current app to use snackbars, in the Google spec they show various ways of using them http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-specs
Example A:
Example B:
Here's my code atm:
Snackbar snackbar = Snackbar.make(mParentLayout, displayMessage, Snackbar.LENGTH_LONG); snackbar.setAction(actionMessage, mClickListener); snackbar.show();
I get the result in Example B,
How can i add margins?
setTextSize(24); //increase max lines of text in snackbar. default is 2. sbNoAct. show(); int height = sbView.
First, you have to create a SnackBar which can be done by calling the following constructor. The only required parameter is content which is the widget to be displayed inside the snackbar. In most cases, the widget is a Text widget, but you can also use other types of Flutter widget.
This example demonstrates how do I use snackBar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
In addition to Saeid's answer, you can get the default SnackBar layout params and modify them as you want:
public static void displaySnackBarWithBottomMargin(Snackbar snackbar, int sideMargin, int marginBottom) { 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); snackbar.show(); }
None of the above solutions worked for me.
But, I got one trick that we can use with the help of translation.
Snackbar snackbar = Snackbar.make(mActivity.getWindow().getDecorView().getRootView(), message, Snackbar.LENGTH_SHORT); View snackBarView = snackbar.getView(); snackBarView.setTranslationY(-(convertDpToPixel(48, mActivity))); snackbar.show();
you can find convertDpToPixel method here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With