How to change background color of the snackbar?
I am showing snackbar
in a DialogFragment
within the positive touch of the alert dialog. Here is my code snippet:
Snackbar snackbar = Snackbar.make(view, "Please enter customer name", Snackbar.LENGTH_LONG)
.setAction("Action", null);
View sbView = snackbar.getView();
sbView.setBackgroundColor(Color.BLACK);
snackbar.show();
I am passing the view of the DialogFragment
to the snackbar. I want the background color to be black. How can I do this? I am returning the alertDialog
in the DialogFragment
. And the theme I am setting to the dialog as follow's:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">@color/accent</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/primary</item>
<!-- Used for the background -->
<item name="android:background">@color/white</item>
</style>
Although I am setting the background color to white for the dialog, it should override by setting the background color to the snackbar.
Solution 1:
Try setting background color like this:
sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));
It will work 100% !
Solution 2:
you can do it like this
Snackbar snackbar;
snackbar = Snackbar.make(view, "Message", Snackbar.LENGTH_SHORT);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(yourColor);
TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(textColor);
snackbar.show();
Solution 3:
As none of the other answers provided a custom style override (that I consider one of the safest update way to do that) I post here my solution.
I post a solution that already address the new AndroidX
(support design 28
) theme.
Provided that your application use a custom them called MyAppTheme
in your AndroidManifest.xml
:
<application
android:name=".MyApplicationName"
android:allowBackup="true"
android:icon="@mipmap/icon"
android:roundIcon="@mipmap/icon_round"
android:label="@string/app_name"
android:theme="@style/MyAppTheme">
Create (if you haven't already) values/style.xml
file overriding the theme used by your application:
<style name="MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/myColorPrimary</item>
<item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
<item name="colorAccent">@color/myColorAccent</item>
<item name="snackbarStyle">@style/MySnackBarStyle</item>
</style>
<!-- snackbar style in res/values -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:background">@color/mySnackbarBackgroundColor</item>
</style>
and provide your colors in your values/colors.xml
file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myColorPrimary">#008577</color>
<color name="myColorPrimaryDark">#00574B</color>
<color name="myColorAccent">#D81B60</color>
<color name="mySnackbarBackgroundColor">#D81B60</color>
</resources>
UPDATE 2020
As the above solution removes the round corner of the snacker bacause setting the background this way uses the legacy snackbar design, if you want to preserve the material design you can.
- If you are targeting API 21+
replace android:background
with android:backgroundTint
<!-- snackbar style in res/values-21/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:backgroundTint">@color/mySnackbarBackgroundColor</item>
</style>
If you are targeting API < 21 then if you decide to use legacy snackbar for API < 21 you could set your abouve
MySnackbarStyle
in res/values-21/ folder and leave the previous — legacy — style in your res/values folder.If you are targeting API < 21 and you want to have the material style of the snackbar also in this lower API levels you can change your snackbar style in your res/values/ this way:
<!-- snackbar style in res/values/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
<item name="android:background">@drawable/my_snackbar_background</item>
</style>
and borrow your my_snackbar_background
from the official repo, this way:
<!-- in res/drawable/ -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp"/>
<solid android:color="@color/mySnackbarBackgroundColor"/>
</shape>
Here is a playground repo.
Solution 4:
Kotlin version (with an extension) :
Create in a file (for exemple SnackbarExtension.kt) an extension :
fun Snackbar.withColor(@ColorInt colorInt: Int): Snackbar{
this.view.setBackgroundColor(colorInt)
return this
}
Next, in your Activity/Fragment, you'll be able to do this :
Snackbar
.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
.withColor(YOUR_COLOR)
.show()