How to change background color of the snackbar?

后端 未结 16 627
温柔的废话
温柔的废话 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:04

    setBackgroundResource() works just as well.

    Snackbar snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
    View sbView = snackbar.getView();
    sbView.setBackgroundResource(R.color.background);
    snackbar.show();
    
    0 讨论(0)
  • 2020-12-07 20:05

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

    Basically, the solutions that were provided have one disadvantage. They change the shape of snackbar and remove the radius.

    Personally, prefer something like that

    val snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
    val view = snackbar.getView();
    val color = view.resources.getColor(colorId)
    view.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    
    0 讨论(0)
  • 2020-12-07 20:08

    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.

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

    Try setting background color like this:

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

    It will work 100% !

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

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