I am showing snackbar in DialogFragment Within the Positive click of alertDialog. Here is my code snippet.
Snackbar snackbar = Snackbar.make(view, \"Please e
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();
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();
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)
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.
Try setting background color like this:
sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));
It will work 100% !
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();