How to change background color of the snackbar? How to change background color of the snackbar? android android

How to change background color of the snackbar?


Try setting background color like this:

sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));

It will work 100% !


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();


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.

  1. 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>
  1. 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.

  2. 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.

enter image description here