Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active November 18, 2017 00:15
Show Gist options
  • Select an option

  • Save voghDev/71bb95a2525e7e9782b4 to your computer and use it in GitHub Desktop.

Select an option

Save voghDev/71bb95a2525e7e9782b4 to your computer and use it in GitHub Desktop.
Example class with Builder pattern that generates custom colored snackbars (android.support.design 22.2.0)
package es.voghdev.examples;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class SSnackbar {
public static class Builder implements CustomSnackbar{
protected String text = "";
protected int duration = Snackbar.LENGTH_SHORT;
protected float textSize;
protected int actionTextColor = 0;
protected int textColor = Color.WHITE;
protected int backgroundColor = Color.BLACK;
OnClickListener action = null;
protected SnackbarStyle style = SnackbarStyle.NORMAL;
protected SnackbarPosition position = SnackbarPosition.BOTTOM;
protected Typeface typeface = null;
protected View rootView;
protected Snackbar snackBar;
public Builder(View rootView){
this.rootView = rootView;
}
public Builder setTextSize(float textSize) {
this.textSize = textSize;
return this;
}
@Override
public Builder setTextColor(int textColor) {
this.textColor = textColor;
return this;
}
@Override
public Builder setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
@Override
public Builder setDuration(int duration) {
this.duration = duration;
return this;
}
@Override
public Builder setStyle(SnackbarStyle style) {
this.style = style;
return this;
}
@Override
public Builder setPosition(SnackbarPosition position) {
this.position = position;
return this;
}
@Override
public Builder setTypeface(Typeface typeface) {
this.typeface = typeface;
return this;
}
@Override
public Builder setText(String text) {
this.text = text;
return this;
}
public Builder setActionTextColor(int actionTextColor) {
this.actionTextColor = actionTextColor;
return this;
}
public void setAction(OnClickListener action) {
this.action = action;
}
public Snackbar build(){
snackBar = Snackbar.make(rootView, text, duration);
styleSnackbar(snackBar);
return snackBar;
}
protected Snackbar styleSnackbar(Snackbar snackbar) {
if(snackbar == null)
throw new IllegalStateException("Snackbar can never be null! Review your Builder implementation");
View snackBarView = snackbar.getView();
if (snackBarView != null) {
TextView snackBarTextView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
snackBarView.setBackgroundColor(backgroundColor);
snackBarTextView.setTextColor(textColor);
snackBarTextView.setTypeface(typeface, style.value());
}
if(actionTextColor != 0)
snackbar.setActionTextColor(actionTextColor);
snackbar.setAction(text, action);
return snackbar;
}
}
}
@voghDev
Copy link
Copy Markdown
Author

voghDev commented Nov 11, 2015

public interface CustomSnackbar {
    public enum SnackbarStyle{
        NORMAL(Typeface.NORMAL),
        BOLD(Typeface.BOLD),
        ITALIC(Typeface.ITALIC),
        BOLD_ITALIC(Typeface.BOLD_ITALIC);

        private final int value;
        private SnackbarStyle(int value){
            this.value = value;
        }

        public int value() { return value; }
    }
    public enum SnackbarPosition{
        BOTTOM, TOP
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment