So I've started using the new Snackbar in the Design Support Library, but I found that when you define "android:textColor" in your theme, it applies to the text color of the snackbar. This is obviously a problem if your primary text color is dark.
Does anyone know a way around this or have advice for how I should color my text?
EDIT January 2017: (Post-Answer)
While there are some custom solutions to fix the problem below, it's probably good to provide the correct way to theme Snackbars.
Firstly, you probably shouldn't be defining android:textColor
in your themes at all (unless you really know the scope of what is using the theme). This sets the text color of basically every view that connects to your theme. If you want to define text colors in your views that are not default, then use android:primaryTextColor
and reference that attribute in your custom views.
However, for applying themes to Snackbar
, please reference this quality guide from a third party material doc: http://www.materialdoc.com/snackbar/ (Follow the programmatic theme implementation to have it not rely on an xml style)
For reference:
// create instance
Snackbar snackbar = Snackbar.make(view, text, duration);
// set action button color
snackbar.setActionTextColor(getResources().getColor(R.color.indigo));
// get snackbar view
View snackbarView = snackbar.getView();
// change snackbar text color
int snackbarTextId = android.support.design.R.id.snackbar_text;
TextView textView = (TextView)snackbarView.findViewById(snackbarTextId);
textView.setTextColor(getResources().getColor(R.color.indigo));
// change snackbar background
snackbarView.setBackgroundColor(Color.MAGENTA);
(You can also create your own custom Snackbar
layouts too, see the above link. Do so if this method feels too hacky and you want a surely reliable way to have your custom Snackbar last through possible support library updates).
And alternatively, see answers below for similar and perhaps faster answers to solve your problem.
One approach is to use spans:
With spans you can also add several colors and styles inside one Snackbar. Here's a nice guide:
https://androidbycode.wordpress.com/2015/06/06/material-design-snackbar-using-the-design-support-library/
I also noticed the same problem. Thanks to the answers here I've created a small class, which can help to solve this problem in more easily, just by replacing this:
with this:
Here is my class:
}
I have a simple code that will help to get an instance of both the textview of Snackbar, after that you can call all methods that are applicable on a textview.
If you are in Kotlin, you can create an extension :
Usage :
If you migrated to androidX use
com.google.android.material.R.id.snackbar_text
instead ofandroid.support.design.R.id.snackbar_text
for changing color of text on snackbar.I found this at What are the new features of Android Design Support Library and how to use its Snackbar?
This worked for me for changing the text color in a Snackbar.