Creating an Snackbar without the need pass view to Snackbar, all layout create in android in android.R.id.content.
public class CustomSnackBar {
public static final int STATE_ERROR = 0;
public static final int STATE_WARNING = 1;
public static final int STATE_SUCCESS = 2;
public static final int VIEW_PARENT = android.R.id.content;
public CustomSnackBar(View view, String message, int actionType) {
super();
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.parseColor("#ffffff"));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
textView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
switch (actionType) {
case STATE_ERROR:
snackbar.getView().setBackgroundColor(Color.parseColor("#F12B2B"));
break;
case STATE_WARNING:
snackbar.getView().setBackgroundColor(Color.parseColor("#000000"));
break;
case STATE_SUCCESS:
snackbar.getView().setBackgroundColor(Color.parseColor("#7ED321"));
break;
}
snackbar.show();
}
}
for call class
new CustomSnackBar(findViewById(CustomSnackBar.VIEW_PARENT),"message", CustomSnackBar.STATE_ERROR);