Skip to content

Instantly share code, notes, and snippets.

@jakubkinst
Created June 25, 2015 09:26
Show Gist options
  • Save jakubkinst/6bf5871eefa0c8be7eea to your computer and use it in GitHub Desktop.
Save jakubkinst/6bf5871eefa0c8be7eea to your computer and use it in GitHub Desktop.
Android Snackbar with background color themed by colorAccent attribute of current theme
package cz.kinst.jakub.themedsnackbar;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.util.TypedValue;
import android.view.View;
/**
* Created by jakubkinst on 25/06/15.
*/
public class ThemedSnackbar {
public static Snackbar make(View view, CharSequence text, int duration) {
Snackbar snackbar = Snackbar.make(view, text, duration);
snackbar.getView().setBackgroundColor(getAttribute(view.getContext(), R.attr.colorAccent));
return snackbar;
}
public static Snackbar make(View view, int resId, int duration) {
return make(view, view.getResources().getText(resId), duration);
}
private static int getAttribute(Context context, int resId) {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(resId, typedValue, true);
return typedValue.data;
}
}
@ignaciotcrespo
Copy link

Worked perfect, thanks! 👍

I needed to setup the action text color also using snackbar.setActionTextColor(getAttribute(view.getContext(), android.R.attr.textColorPrimary));

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