Ajinkya Ajinkya - 1 year ago 165
Android Question

How to change background color of the snackbar?

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

As u can see my snackbars background color showing white color

I am passing view of the dialogfragment to the snackbar.I want the background color black?how can i do this?Pardon for the my bad english.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>

Although i am setting background color to white for dialog it should override by setting the background color to the snackbar.Please sorry for bad English.

Answer Source

Try setting background color like this:

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

It will work 100% !