Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color of the snackbar?

I am showing snackbar in a DialogFragment within the positive touch of the alert dialog. 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(); 

As you can see my snackbars background color is showing white color

I am passing the view of the DialogFragment to the snackbar. I want the background color to be black. How can I do this? I am returning the 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 the background color to white for the dialog, it should override by setting the background color to the snackbar.

like image 250
Ajinkya Avatar asked Dec 01 '15 12:12

Ajinkya


People also ask

How do you change the background color on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How do I use Snackbar?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.


1 Answers

Try setting background color like this:

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

It will work 100% !

like image 101
Dusan Dimitrijevic Avatar answered Sep 20 '22 20:09

Dusan Dimitrijevic