Reputation: 2524
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"?
Upvotes: 6
Views: 7519
Reputation: 364780
You can just use the default Snackbar
in the Material Components Library:
If you want to change the margin of the Snackbar
you can add in your app theme the snackbarStyle
attribute:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="snackbarStyle">@style/MySnackbar</item>
</style>
with:
<style name="MySnackbar" parent="Widget.MaterialComponents.Snackbar">
<item name="android:layout_margin">32dp</item>
</style>
Upvotes: 0
Reputation: 3080
I added those styles from https://material.io/develop/android/components/snackbars/
<style name="Theme.App" parent="Theme.MaterialComponents.*">
...
<item name="snackbarStyle">@style/Widget.App.Snackbar</item>
<item name="snackbarButtonStyle">@style/Widget.App.SnackbarButton</item>
</style>
<style name="Widget.App.Snackbar" parent="Widget.MaterialComponents.Snackbar">
<item name="materialThemeOverlay">@style/ThemeOverlay.App.Snackbar</item>
<item name="actionTextColorAlpha">1</item>
</style>
<style name="Widget.App.SnackbarButton" parent="Widget.MaterialComponents.Button.TextButton.Snackbar">
<item name="android:textColor">@color/shrine_pink_100</item>
</style>
<style name="ThemeOverlay.App.Snackbar" parent="">
<item name="colorPrimary">@color/shrine_pink_100</item>
<item name="colorOnSurface">@color/shrine_pink_900</item>
</style>
and it works! Modifying code/layout/background programmatically didn't work work me
Upvotes: 1
Reputation: 4879
You need to use the new Material theme.
In your build.gradle import
implementation 'com.google.android.material:material:1.2.0-alpha06'
Change all your Snackbar import to:
import com.google.android.material.snackbar.Snackbar
Then in your style.xml file your parent theme needs to be one of the material themes:
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight.NoActionBar
Upvotes: 1
Reputation: 41
There are only three things you need to do to build a rounded SnackBar like Material:
Here is the complete code:
Snackbar.make(
findViewById(R.id.coordinator_layout),
"Your text here",
Snackbar.LENGTH_SHORT
).apply {
view.layoutParams = (view.layoutParams as CoordinatorLayout.LayoutParams).apply {
setMargins(
12,
12,
12,
12
)
}
view.background = resources.getDrawable(R.drawable.round_corner, null)
}.show()
and here is the code for round corner drawable in drawable/round_corner:
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#323232"/>
<corners android:radius="4dp"/>
</shape>
Upvotes: 2
Reputation: 2011
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();
}
Upvotes: 3