How to change background color of the snackbar?

2019-03-08 18:11发布

问题:

I am showing snackbar in DialogFragment Within the Positive click of alertDialog. 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 view of the dialogfragment to the snackbar. I want the background color black? How can I do this? I am returning 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 background color to white for dialog it should override by setting the background color to the snackbar.

回答1:

Try setting background color like this:

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

It will work 100% !



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


回答3:

If you want to define a background color for all your Snackbars, just override the design_snackbar_background_color value somewhere in your resources. For example:

<color name="design_snackbar_background_color" tools:override="true">@color/colorPrimaryLight</color>


回答4:

Bellow code is useful for change the text color of message.

Snackbar snackbar = Snackbar.make(rootView, "Enter Your Message",Snackbar.LENGTH_SHORT);
View view = snackbar.getView();
TextView tv = (TextView)view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();

Second Way: You can change color by changing theme of activity also.



回答5:

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


回答6:

It's too late but In case someone still needs help. Here is the working solution.

      Snackbar snackbar = Snackbar.make(mainView, text, Snackbar.LENGTH_LONG);
    View snackBarView = snackbar.getView();
    snackBarView.setBackgroundColor(context.getResources().getColor(R.color.btn_background_color));
    snackbar.show();


回答7:

While Working with xamarin android I found out that ContextCompat.GetColor() returns Int but the setBackgroundColor() expects a Parameter of type Color. So here is how I got it working in my xamarin android project.

Snackbar snackbarview =  Snackbar.Make(toolbar, message, Snackbar.LengthLong);
View snckView = snackbarview.View;                
snckView.SetBackgroundColor(Color.ParseColor(GetString(Resource.Color.colorPrimary)));
snackbarview.Show();


回答8:

I made a little utils class so I can easily make custom colored snackbars thru out the app.

package com.yourapppackage.yourapp;

import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SnackbarUtils {

    private int BACKGROUND_COLOR;
    private int TEXT_COLOR;
    private int BUTTON_COLOR;
    private String TEXT;


    public SnackbarUtils(String aText, int aBgColor, int aTextColor, int aButtonColor){
        this.TEXT = aText;
        this.BACKGROUND_COLOR = aBgColor;
        this.TEXT_COLOR = aTextColor;
        this.BUTTON_COLOR = aButtonColor;
    }

    public Snackbar snackieBar(){
        Snackbar snackie = Snackbar.make(MainActivity.getInstance().findViewById(android.R.id.content), TEXT, Snackbar.LENGTH_LONG);
        View snackView = snackie.getView();
        TextView snackViewText = (TextView) snackView.findViewById(android.support.design.R.id.snackbar_text);
        Button snackViewButton = (Button) snackView.findViewById(android.support.design.R.id.snackbar_action);
        snackView.setBackgroundColor(BACKGROUND_COLOR);
        snackViewText.setTextColor(TEXT_COLOR);
        snackViewButton.setTextColor(BUTTON_COLOR);
        return snackie;
    }
}

then to use it, like this any where in the app:

new SnackbarUtils("This is the text displayed", Color.RED, Color.BLACK, Color.YELLOW).snackieBar().setAction("OTAY", v -> { 
     //donothing
     }).show();


回答9:

Put it in an Utility class:

public class Utility {
    public static void showSnackBar(Context context, View view, String text) {
        Snackbar sb = Snackbar.make(view, text, Snackbar.LENGTH_SHORT);
        sb.getView().setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
        sb.show();
    }
}

Using like this:

Utility.showSnackBar(getApplicationContext(), findViewById(android.R.id.content), "Add success!!!");


回答10:

As none of the other answers provided a custom style override (that I consider one of the more update safe 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 calle 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 -->
<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>


回答11:

public class CustomBar {

public static void show(View view, String message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

public static void show(View view, @StringRes int message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

}



回答12:

None of other solutions really worked for me. If I only set Snackbar's background color, the layout under TextView and Button was in default color. If I set TextView's background it did a little blink after SnackBar was shown. And layout around button was still in default color.

At the end I found out that the best way for me is to change background color of TextView's parent (SnackbarContentLayout). Now whole Snackbar is colored properly and it doesn't blink when it shows up.

snack = Snackbar.make(view, text, duration)
View view = snack.getView();
view.setBackgroundColor(BACKGROUND_COLOR);
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(TEXT_COLOR);
((SnackbarContentLayout) tv.getParent()).setBackgroundColor(BACKGROUND_COLOR);