qwertz
qwertz

Reputation: 6286

Snackbar action text color not changing

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();

Upvotes: 47

Views: 28398

Answers (5)

HAZEEM JOONUS
HAZEEM JOONUS

Reputation: 583

Try this,

 Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Permission required!", 3000 /*Snackbar.LENGTH_INDEFINITE*/);
 snackbar.setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // perform any action when the button on the snackbar is clicked
            Toast.makeText(MainActivity.this, "Permission granted.", Toast.LENGTH_SHORT).show();
        }
    });
 snackbar.setBackgroundTint(getResources().getColor(R.color.black));      // set the background tint color for the snackbar
 snackbar.setActionTextColor(getResources().getColor(R.color.purple_500)); // set the action button text color
 snackbar.show();

Upvotes: 0

Mahbubur Rahman Khan
Mahbubur Rahman Khan

Reputation: 415

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));

Upvotes: 1

Anand Singh
Anand Singh

Reputation: 5692

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).

Upvotes: 93

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();

Upvotes: 7

Adam Purser
Adam Purser

Reputation: 331

Use

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

instead of just

.setActionTextColor(R.color.red)

Upvotes: 28

Related Questions