Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active September 20, 2020 17:56
Show Gist options
  • Save WrathChaos/36549e27316076d4add8 to your computer and use it in GitHub Desktop.
Save WrathChaos/36549e27316076d4add8 to your computer and use it in GitHub Desktop.
Custom colorful snackbar on Android. Article: https://freakycoder.com/android-notes-9-colorful-snackbar-261089d88f05
public class ColorfulSnackbar {
    
    private static final int red = Color.parseColor("#f41515");
    private static final int green = 0xff4caf50;
    private static final int blue = 0xff2195f3;
    private static final int orange = 0xffffc107;
    private static final int myBlue = Color.parseColor("#002487");

    private static View getSnackBarLayout(Snackbar snackbar) {
        if (snackbar != null) {
            return snackbar.getView();
        }
        return null;
    }

    private static Snackbar colorSnackBar(final Snackbar snackbar, int colorId) {
        View snackBarView = getSnackBarLayout(snackbar);
        if (snackBarView != null) {
            snackBarView.setBackgroundColor(myBlue);
            // Changing action button text color
            View sbView = snackbar.getView();
            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.WHITE);
        }
        return snackbar;
    }

    public static Snackbar info(Snackbar snackbar) {
        return colorSnackBar(snackbar, blue);
    }

    public static Snackbar warning(Snackbar snackbar) {
        return colorSnackBar(snackbar, orange);
    }

    public static Snackbar alert(Snackbar snackbar) {
        return colorSnackBar(snackbar, red);
    }

    public static Snackbar confirm(Snackbar snackbar) {
        return colorSnackBar(snackbar, green);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment