How to change background color of the snackbar?

后端 未结 16 629
温柔的废话
温柔的废话 2020-12-07 19:28

I am showing snackbar in DialogFragment Within the Positive click of alertDialog. Here is my code snippet.

Snackbar snackbar = Snackbar.make(view, \"Please e         


        
相关标签:
16条回答
  • 2020-12-07 20:14

    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);
    
    0 讨论(0)
  • 2020-12-07 20:19
    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();
    }
    

    }

    0 讨论(0)
  • 2020-12-07 20:20

    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();
    
    0 讨论(0)
  • 2020-12-07 20:27

    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();
    
    0 讨论(0)
提交回复
热议问题