How to set support library snackbar text color to something other than android:textColor?
Solution 1
I know this has been answered already but the easiest way I found was directly in the make using the Html.fromHtml
method and a font
tag
Snackbar.make(view,
Html.fromHtml("<font color=\"#ffffff\">Tap to open</font>").show()
Solution 2
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.
Snackbar snack = Snackbar.make(view, R.string.message, Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snack.show();
UPDATE: ANDROIDX:
As dblackker points out in the comments, with the new AndroidX support library, code to find the ID of Snackbar TextView changes to:
TextView tv = view.findViewById(com.google.android.material.R.id.snackbar_text);
tv.setTextColor(ContextCompat.getColor(requireContext(), R.color.someColor))
Solution 3
Created this kotlin extention function i use in my projects:
fun Snackbar.setTextColor(color: Int): Snackbar {
val tv = view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
tv.setTextColor(color)
return this
}
Usage like you would expect:
Snackbar.make(view, R.string.your_string,Snackbar.LENGTH_LONG).setTextColor(Color.WHITE).show()
Solution 4
Alright so I fixed it by basically reorganizing the way I do text colors.
In my light theme, I set android:textColorPrimary
to the normal dark text
I wanted, and I set android:textColor
to white
.
I updated all of my text views and buttons to have android:textColor="?android:attr/textColorPrimary".
So because snackbar draws from textColor
, I just set all of my other text to textColorPrimary
.
EDIT JANUARY 2017: ----------------------------------------------------
So as the comments say, and as stated in the edited original question above, you should probably not define android:textColor
in your themes, as this changes the text color of every view inside the theme.
Solution 5
Hacking on android.support.design.R.id.snackbar_text
is fragile, a better or less hacky way to do that will be:
String snackText = getResources().getString(YOUR_RESOURCE_ID);
SpannableStringBuilder ssb = new SpannableStringBuilder()
.append(snackText);
ssb.setSpan(
new ForegroundColorSpan(Color.WHITE),
0,
snackText.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Snackbar.make(
getView(),
ssb,
Snackbar.LENGTH_SHORT)
.show();
Related videos on Youtube
Mahonster
Individual Android Developer seeking to make good in the world of programming. Experience isn't on my strong side yet, but it will be someday. Apologies if I'm too slow witted in my questions and responses too, that's the Texas side of me.
Updated on August 18, 2021Comments
-
Mahonster over 2 years
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 useandroid: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.
-
muetzenflo over 5 yearsthanks for the solution! the property is actually called
android:textColorPrimary
-
Bugs Happen about 5 yearsThank you for this comprehensive explanation.
-
Tyler over 4 years"you probably shouldn't be defining android:textColor in your themes at all..." that was the key for me, thank you!
-
-
BrantApps over 8 yearsI've just completed this task, phew, took a while! However, I needed to use "m vai's" method below for my antiquated Preferences activity (using preferences.xml). I think it's left my app's styling far more correctly theme based which is nice. +1
-
Janis Peisenieks over 8 yearsJust in case anyone else is futilely searching for a way to change the text size of a Snackbar, this is it!
-
angel about 8 yearssnack.getView()? what is snack object?
-
angel about 8 yearsI believe I have got the complete code View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content); Snackbar mysnack= Snackbar.make(rootView, Mensaje, Snackbar.LENGTH_LONG); View view= mysnack.getView(); TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.WHITE); mysnack.show();
-
Joshua Pinter about 8 yearsFYI, you can do the same with the Action text by using
snackbar_action
instead ofsnackbar_text
. See this answer for an example: stackoverflow.com/a/36800101/293280 -
DiscDev about 8 yearsThis is a hack at best - what happens when the ID for android.support.design.R.id.snackbar_text changes in a future release of the support library? Is there not some kind of default SnackBar style that can be overridden in styles.xml?
-
DiscDev about 8 yearsThis is a hack, and relies on the id's of various support library views to be static. I would avoid this solution at all costs.
-
CaptainForge almost 8 yearsThat ID was indeed changed in a future release of the support library, I'm getting a null pointer now.
-
issamux over 7 yearsNot a good solution IMO , i wanna keep my actual theme for textview and just change snakbar text color...
-
Eugen Pechanec over 7 yearsYou're not supposed to define
android:textColor
in your theme at all. It's a style attribute ofTextView
. If you define it in your theme you override text color of everyTextView
orButton
which doesn't specify its text color in XML. That also happens to be the snackbar message which would normally take color from its dark theme overlay. -
Lahiru Chandima over 7 yearsI prefer this solution since it is single line.
-
JPM over 7 yearsThis is not a good answer for future compat. Answer below is much better, stackoverflow.com/a/37309267/346309
-
Ahamadullah Saikat over 6 yearsThis is actual solution of that question. I was searching for that types of answer. Thanks.
-
Ahamadullah Saikat over 6 yearsThis is not the actual solution of that question. Use: snackbar.setActionTextColor(Color.WHITE).show();
-
JPM over 6 yearsThere is/was a bug in the Snackbar setting the color did nothing. This way works no matter what but may not be the optimal solution for others.
-
Patrick Kuijpers about 6 yearsLove the simple use of Kotlin extention methods! ;-)
-
Emmanuel Guther almost 6 yearsDo not use this please, it is very insecure.
-
Rishabh876 over 5 yearsUse
com.google.android.material.R.id.snackbar_text
if you migrate to AndroidX. -
Richard over 5 yearssame answer as mine 😉
-
dblackker over 5 yearsUpdate - with androidx, the id is now as follows:
snack.view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
-
frank17 about 5 yearsU deserve a medal.
-
frank17 about 5 yearsThis requires API level >24.
-
CoolMind over 4 years@frank17, it was mentioned in several comments here.
-
Richard over 3 yearsChanged @Rishabh876 :)
-
Jerry Okafor almost 3 yearsWhy is this not getting more upvotes? It works just fine.
-
Dev I.A over 2 yearsthis best solution, btw