Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ColoredSnackBar

You can use this class to customize the SnackBar and to highlight the type of message.

Screen

Example:

  //Info SnackBar
  Snackbar snackbar = Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                              .setAction("Action", null);
  ColoredSnackBar.info(snackbar);
  snackbar.show();  

  //Confirm SnackBar     
  Snackbar snackbar = Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                              .setAction("Action", null);
  ColoredSnackBar.confirm(snackbar);  
  snackbar.show();

  //Alert SnackBar
  Snackbar snackbar = Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                              .setAction("Action", null);
  ColoredSnackBar.error(snackbar);
   snackbar.show();
public class ColoredSnackBar {
private static final int red = 0xfff44336;
private static final int green = 0xff4caf50;
private static final int blue = 0xff2195f3;
private static final int orange = 0xffffc107;
private static View getSnackBarLayout(Snackbar snackbar) {
if (snackbar != null) {
return snackbar.getView();
}
return null;
}
private static void colorSnackBar(Snackbar snackbar, int colorId) {
View snackBarView = getSnackBarLayout(snackbar);
if (snackBarView != null) {
snackBarView.setBackgroundColor(colorId);
}
}
public static void info(Snackbar snackbar) {
colorSnackBar(snackbar, blue);
}
public static void warning(Snackbar snackbar) {
colorSnackBar(snackbar, orange);
}
public static void error(Snackbar snackbar) {
colorSnackBar(snackbar, red);
}
public static void confirm(Snackbar snackbar) {
colorSnackBar(snackbar, green);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment