Skip to content

Commit

Permalink
[Snackbar] Add support for changing text & background tint color at r…
Browse files Browse the repository at this point in the history
…untime

Resolves #360

GIT_ORIGIN_REV_ID=ced4885fd03d6117d5ee460253184d1605895e55
PiperOrigin-RevId: 248537604
  • Loading branch information
ricknout authored and leticiarossi committed May 16, 2019
1 parent 7cb8af2 commit d7e994a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/java/com/google/android/material/snackbar/Snackbar.java
Expand Up @@ -23,6 +23,9 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
Expand All @@ -31,6 +34,7 @@
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.graphics.drawable.DrawableCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -331,6 +335,30 @@ public int getDuration() {
: super.getDuration();
}

/**
* Sets the text color of the message specified in {@link #setText(CharSequence)} and {@link
* #setText(int)}.
*/
@NonNull
public Snackbar setTextColor(ColorStateList colors) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setTextColor(colors);
return this;
}

/**
* Sets the text color of the message specified in {@link #setText(CharSequence)} and {@link
* #setText(int)}.
*/
@NonNull
public Snackbar setTextColor(@ColorInt int color) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setTextColor(color);
return this;
}

/**
* Sets the text color of the action specified in {@link #setAction(CharSequence,
* View.OnClickListener)}.
Expand All @@ -355,6 +383,22 @@ public Snackbar setActionTextColor(@ColorInt int color) {
return this;
}

/** Sets the tint color of the background Drawable. */
@NonNull
public Snackbar setBackgroundTint(@ColorInt int color) {
Drawable background = view.getBackground();
if (background != null) {
// Drawable doesn't implement setTint in API 21 and Snackbar does not yet use
// MaterialShapeDrawable as its background (i.e. TintAwareDrawable)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
DrawableCompat.setTint(background, color);
} else {
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
return this;
}

/**
* Set a callback to be called when this the visibility of this {@link Snackbar} changes. Note
* that this method is deprecated and you should use {@link #addCallback(BaseCallback)} to add a
Expand Down

0 comments on commit d7e994a

Please sign in to comment.