How to set support library snackbar text color to something other than android:textColor?

45,872

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();
Share:
45,872

Related videos on Youtube

Mahonster
Author by

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, 2021

Comments

  • Mahonster
    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.

    enter image description here

    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.

    • muetzenflo
      muetzenflo over 5 years
      thanks for the solution! the property is actually called android:textColorPrimary
    • Bugs Happen
      Bugs Happen about 5 years
      Thank you for this comprehensive explanation.
    • Tyler
      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
    BrantApps over 8 years
    I'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
    Janis Peisenieks over 8 years
    Just in case anyone else is futilely searching for a way to change the text size of a Snackbar, this is it!
  • angel
    angel about 8 years
    snack.getView()? what is snack object?
  • angel
    angel about 8 years
    I 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
    Joshua Pinter about 8 years
    FYI, you can do the same with the Action text by using snackbar_action instead of snackbar_text. See this answer for an example: stackoverflow.com/a/36800101/293280
  • DiscDev
    DiscDev about 8 years
    This 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
    DiscDev about 8 years
    This 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
    CaptainForge almost 8 years
    That ID was indeed changed in a future release of the support library, I'm getting a null pointer now.
  • issamux
    issamux over 7 years
    Not a good solution IMO , i wanna keep my actual theme for textview and just change snakbar text color...
  • Eugen Pechanec
    Eugen Pechanec over 7 years
    You're not supposed to define android:textColor in your theme at all. It's a style attribute of TextView. If you define it in your theme you override text color of every TextView or Button 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
    Lahiru Chandima over 7 years
    I prefer this solution since it is single line.
  • JPM
    JPM over 7 years
    This is not a good answer for future compat. Answer below is much better, stackoverflow.com/a/37309267/346309
  • Ahamadullah Saikat
    Ahamadullah Saikat over 6 years
    This is actual solution of that question. I was searching for that types of answer. Thanks.
  • Ahamadullah Saikat
    Ahamadullah Saikat over 6 years
    This is not the actual solution of that question. Use: snackbar.setActionTextColor(Color.WHITE).show();
  • JPM
    JPM over 6 years
    There 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
    Patrick Kuijpers about 6 years
    Love the simple use of Kotlin extention methods! ;-)
  • Emmanuel Guther
    Emmanuel Guther almost 6 years
    Do not use this please, it is very insecure.
  • Rishabh876
    Rishabh876 over 5 years
    Use com.google.android.material.R.id.snackbar_text if you migrate to AndroidX.
  • Richard
    Richard over 5 years
    same answer as mine 😉
  • dblackker
    dblackker over 5 years
    Update - with androidx, the id is now as follows: snack.view.findViewById(com.google.android.material.R.id.sna‌​ckbar_text) as TextView
  • frank17
    frank17 about 5 years
    U deserve a medal.
  • frank17
    frank17 about 5 years
    This requires API level >24.
  • CoolMind
    CoolMind over 4 years
    @frank17, it was mentioned in several comments here.
  • Richard
    Richard over 3 years
    Changed @Rishabh876 :)
  • Jerry Okafor
    Jerry Okafor almost 3 years
    Why is this not getting more upvotes? It works just fine.
  • Dev I.A
    Dev I.A over 2 years
    this best solution, btw