Created
June 25, 2015 09:26
-
-
Save jakubkinst/6bf5871eefa0c8be7eea to your computer and use it in GitHub Desktop.
Android Snackbar with background color themed by colorAccent attribute of current theme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked perfect, thanks! 👍
I needed to setup the action text color also using
snackbar.setActionTextColor(getAttribute(view.getContext(), android.R.attr.textColorPrimary));