How to set the text color of a library's procurement library for something other than android: textColor?

So, I started using the new Snackbar in the design support library, but I found that when you define “android: textColor” in your theme, this refers to the color of the text of the zakut. This is obviously a problem if your primary text color is dark.

enter image description here

Does anyone know a way around this or advise me how to color the text?

EDIT January 2017: (post-reply)

Although there are some specific solutions to fix the problem below, it is probably useful to provide the correct path to Snackbars themes.

First, you probably shouldn't define android:textColor in your themes at all (unless you know what the topic is). This sets the color of the text mainly for each view that connects to your theme. If you want to define text colors in your views that are not standard, use android:primaryTextColor and specify this attribute in your custom views.

However, to apply themes to Snackbar refer to this quality guide from a third-party material document: http://www.materialdoc.com/snackbar/ (Follow the implementation of the program theme so that it does not depend on the 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 Snackbar layouts, see the link above. Do it if this method is too hacked and you want a reliable way to update your custom Snackbar with possible support library updates).

And, alternatively, see the answers below for similar and possibly faster answers to solve your problem.

+86
android android-support-library android-design-library snackbar
Jun 25 '15 at 21:55
source share
20 answers

I know that this question has already been answered, but the easiest way I found was directly in make using the Html.fromHtml method and the font tag

 Snackbar.make(view, Html.fromHtml("<font color=\"#ffffff\">Tap to open</font>").show() 
+35
May 18 '16 at 20:13
source share

I found out about this in the "What's New in Android Design Support Library and How to Use It Snackbar?"

This helped me change the color of the text in the snack bar.

 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, the code for finding the Snackbar TextView ID is changed to:

 TextView tv = snack.findViewById(com.google.android.material.R.id.snackbar_text); 
+152
Jun 27 '15 at 2:26
source share

Created this kotlin extension function that I use in my projects:

 fun Snackbar.setTextColor(color: Int): Snackbar { val tv = view.findViewById(android.support.design.R.id.snackbar_text) as TextView tv.setTextColor(color) return this } 

Use as you expect:

Snackbar.make (view, R.string.your_string, Snackbar.LENGTH_LONG) .setTextColor (Color.WHITE) .show ()

+15
Jan 08 '18 at 8:08
source share

Ok, so I fixed it, basically reorganizing the way I paint the colors of the text.

In my easy topic, I set android:textColorPrimary to normal dark text , I wanted it, and I set android:textColor to white .

I updated all my text views and buttons to have android:textColor="?android:attr/textColorPrimary".

Since snackbar draws from textColor , I just set all my other text to textColorPrimary .

EDIT JANUARY 2017: ---------------------------------------- ------ ------

So, as the comments say, and as stated in the edited source question above, you probably shouldn't define android:textColor in your themes, as this changes the color of the text of each view within the theme.

+13
Jun 25 '15 at 10:50
source share

Hacking on android.support.design.R.id.snackbar_text is a fragile, better or less hacky way to do this would 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(); 
+13
May 13 '16 at 17:32
source share

One approach is to use intervals:

 final ForegroundColorSpan whiteSpan = new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.white)); SpannableStringBuilder snackbarText = new SpannableStringBuilder("Hello, I'm white!"); snackbarText.setSpan(whiteSpan, 0, snackbarText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG) .show(); 

With spans, you can also add several colors and styles inside one appetizer. Here is a good guide:

https://androidbycode.wordpress.com/2015/06/06/material-design-snackbar-using-the-design-support-library/

+10
Oct 22 '15 at 11:13
source share

If you switched to androidX, use com.google.android.material.R.id.snackbar_text instead of android.support.design.R.id.snackbar_text to change the color of the text in the panel.

+9
Jan 17 '19 at 11:17
source share

The only way I can see is to use getView() and ride the bike through its child. I do not know if this will work, and it is bad, as it seems. Hopefully they will add an API soon on this issue.

 Snackbar snack = Snackbar.make(...); ViewGroup group = (ViewGroup) snack.getView(); for (int i = 0; i < group.getChildCount(); i++) { View v = group.getChildAt(i); if (v instanceof TextView) { TextView t = (TextView) v; t.setTextColor(...) } } snack.show(); 
+6
Jun 25 '15 at 22:48
source share

If you port your code to AndroidX, the TextView property now:

 com.google.android.material.R.id.snackbar_text 
+6
Feb 22 '19 at 22:55
source share

I changed the subject

 Theme.AppCompat.Light.NoActionBar 

to

 Theme.AppCompat.NoActionBar 

It worked. Try using a simple theme instead of a light or different theme.

+2
Oct 21 '15 at 9:14
source share

This is what I use when I need custom colors

  @NonNull public static Snackbar makeSnackbar(@NonNull View layout, @NonNull CharSequence text, int duration, int backgroundColor, int textColor/*, int actionTextColor*/){ Snackbar snackBarView = Snackbar.make(layout, text, duration); snackBarView.getView().setBackgroundColor(backgroundColor); //snackBarView.setActionTextColor(actionTextColor); TextView tv = (TextView) snackBarView.getView().findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(textColor); return snackBarView; } 

And consumed as:

 CustomView.makeSnackbar(view, "Hello", Snackbar.LENGTH_LONG, Color.YELLOW,Color.CYAN).setAction("DO IT", myAction).show(); 
+2
Jan 10 '16 at 1:18
source share

If you decide to use a dirty and hacky solution to search for TextView in Snackbar by id, and you have already migrated to androidx, then here is the code:

 val textViewId = com.google.android.material.R.id.snackbar_text val snackbar = Snackbar.make(view, "Text", Snackbar.LENGTH_SHORT) val textView = snackbar.view.findViewById(textViewId) as TextView textView.setTextColor(Color.WHITE) 
+2
Dec 16 '18 at 17:22
source share

You can use this library: https://github.com/SandroMachado/restaurant

 new Restaurant(MainActivity.this, "Snackbar with custom text color", Snackbar.LENGTH_LONG) .setTextColor(Color.GREEN) .show(); 

Disclaimer: I created a library.

+1
Nov 28 '15 at 1:04
source share

I have a simple code that will help you get an instance as a Snackbar text view, after which you can call all the methods that apply to the text view.

 Snackbar snackbar = Snackbar.make( ... ) // Create Snack bar snackbar.setActionTextColor(getResources().getColor(R.color.white)); //if you directly want to apply the color to Action Text TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action ); snackbarActionTextView.setTextColor(Color.RED); //This is another way of doing it snackbarActionTextView.setTypeface(snackbarActionTextView.getTypeface(), Typeface.BOLD); //Below Code is to modify the Text in Snack bar TextView snackbarTextView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); snackbarTextView.setTextSize( 16 ); snackbarTextView.setTextColor(getResources().getColor(R.color.white)); 
0
Nov 16 '16 at 10:14
source share
 Snackbar.make(getView(), "Hello", Snackbar.LENGTH_SHORT) .setActionTextColor(Color.WHITE) .show(); 
0
Jul 29 '17 at 19:07
source share

Just to save your valuable development time, here is the static method I'm using:

 public static void snack(View view, String message) { if (!TextUtils.isEmpty(message)) { Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT); snackbar.getView().setBackgroundColor(Color.YELLOW); TextView tv = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); //snackbar_text tv.setTextColor(Color.BLACK); snackbar.show(); } } 

Here's what it looks like:

yellow snackbar with black text

0
Jun 26 '18 at 16:31
source share

If you are in Kotlin , you can create an extension:

 fun Snackbar.withTextColor(color: Int): Snackbar { val tv = this.view.findViewById(android.support.design.R.id.snackbar_text) as TextView tv.setTextColor(color) return this } 

Usage :

 yourSnackBar.withTextColor(Color.WHITE).show() 
0
Jul 18 '18 at 2:59
source share

Searching by id does not work for me, so I found another solution:

 Snackbar snackbar = Snackbar.make(view, text, duration);//just ordinary creation ViewGroup snackbarView = (ViewGroup) snackbar.getView(); SnackbarContentLayout contentLayout = (SnackbarContentLayout) snackbarView.getChildAt(0); TextView tvText = contentLayout.getMessageView(); tvText.setTextColor(/*your color here*/); //set another colors, show, etc 
0
Jul 12 '19 at 18:59
source share

According to new AndroidX Jitpack components

 implementation 'com.google.android.material:material:1.0.0' 

Use this extension that I created

 inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) { val snack = Snackbar.make(this, message, length) snack.f() snack.show() } fun Snackbar.action(action: String, actionColor: Int? = null, textColor: Int? = null, listener: (View) -> Unit) { setAction(action, listener) actionColor?.let { setActionTextColor(it) } textColor?.let { this.view.findViewById<TextView>(R.id.snackbar_text).setTextColor(it) } } 

Use it like that

 btn_login.snack( getString(R.string.fields_empty_login), ContextCompat.getColor(this@LoginActivity, R.color.whiteColor) ) { action(getString(R.string.text_ok), ContextCompat.getColor(this@LoginActivity, R.color.gray_300),ContextCompat.getColor(this@LoginActivity, R.color.yellow_400)) { this@snack.dismiss() } } 
0
Jul 16 '19 at 6:05
source share

I also noticed the same problem. Thanks to the answers here, I created a small class that can help solve this problem more easily by simply replacing this:

 Snackbar.make(view, "Error", Snackbar.LENGTH_LONG).show(); 

with this:

 Snackbar2.make(view, "Error", Snackbar.LENGTH_LONG).show(); 

Here is my class:

 public class Snackbar2 { static public Snackbar make(View view, int resid, int duration){ Snackbar result = Snackbar.make(view, resid, duration); process(result); return result; } static public Snackbar make(View view, String text, int duration){ Snackbar result = Snackbar.make(view, text, duration); process(result); return result; } static private void process(Snackbar snackbar){ try { View view1= snackbar.getView(); TextView tv = (TextView) view1.findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.WHITE); }catch (Exception ex) { //inform about error ex.printStackTrace(); } } 

}

-one
Mar 26 '16 at 10:24
source share



All Articles