Snackbar action text color not changing

24,905

Solution 1

The argument of setActionTextColor is the int that represents the color, not the resource ID.

Instead of this:

.setActionTextColor(R.color.yellow)

try:

.setActionTextColor(Color.YELLOW)

If you want to use resources anyway, try:

.setActionTextColor(ContextCompat.getColor(context, R.color.color_name));

Note: To use ContextCompat, I assume you have included Support library to your build.gradle file (It is optional if you have already appcompat (v7) library too).

Solution 2

Use

.setActionTextColor(getResources().getColor(R.color.red))

instead of just

.setActionTextColor(R.color.red)

Solution 3

None of above answers helped me. I found this solution, and it works by changing manually the TextView's text color

Snackbar snack = Snackbar.make(v, "Snackbar 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();

Solution 4

If you want to change action button text color..

snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));

If you want to change action button background color..

View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));
Share:
24,905
qwertz
Author by

qwertz

Updated on July 09, 2022

Comments

  • qwertz
    qwertz almost 2 years

    I want to change the action text color for my snackbar, but it is not working for some reason.

    I use the following code to display a snackbar:

    Snackbar.make(findViewById(R.id.root), "text", Snackbar.LENGTH_LONG).setActionTextColor(R.color.yellow).setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    }).show();
    
  • Hafez Divandari
    Hafez Divandari over 8 years
    getColor(int) is deprecated use ContextCompat.getColor(context, R.color.red) instead.
  • Prasad
    Prasad almost 8 years
    snackbar.setActionTextColor(getResources().getColor(R.color.‌​colorPrimary)); Worked for me....!
  • tir38
    tir38 over 7 years
    This only works if the the OP's "definition" of yellow is the same as the system.
  • Rafi Panoyan
    Rafi Panoyan about 6 years
    About the getColor being deprecated, don't use @SuppressWarning but instead ContextCompat.getColor(context, R.color.youColor)
  • mtsahakis
    mtsahakis over 5 years
    If anyone wants to use the same technique for changing the action button just add something like TextView action = view.findViewById(android.support.design.R.id.snackbar_actio‌​n); action.setTextColor(view.getContext().getResources().getColo‌​r(android.R.color.ho‌​lo_red_dark));
  • Ricard
    Ricard about 5 years
    You are assigning a color resource reference to the Snackbar (@ColorRes), but if you look at the method #setActionTextColor is requesting a @ColorInt Integer which basically asks a hexadecimal repesentation of a color directly.