From 10edcd11d527996c22d29ce2d0c95ab6533eee73 Mon Sep 17 00:00:00 2001 From: pubiqq Date: Thu, 27 Jan 2022 09:29:08 -0800 Subject: [PATCH] [ChipDrawable] Support setting text color programmatically. Resolves https://github.com/material-components/material-components-android/pull/2526 GIT_ORIGIN_REV_ID=d1f47b5aa0b823ae0915d4c7ae85417e7d0eddd5 PiperOrigin-RevId: 424632069 --- .../android/material/chip/ChipDrawable.java | 21 +++++++++++++++++++ .../material/chip/res/values/attrs.xml | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/java/com/google/android/material/chip/ChipDrawable.java b/lib/java/com/google/android/material/chip/ChipDrawable.java index e0bae333351..60c75850f81 100644 --- a/lib/java/com/google/android/material/chip/ChipDrawable.java +++ b/lib/java/com/google/android/material/chip/ChipDrawable.java @@ -42,6 +42,7 @@ import android.graphics.drawable.RippleDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; +import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import androidx.appcompat.content.res.AppCompatResources; import android.text.TextUtils; @@ -358,6 +359,14 @@ private void loadFromAttributes( float textSize = a.getDimension( R.styleable.Chip_android_textSize, textAppearance.getTextSize()); textAppearance.setTextSize(textSize); + + if (VERSION.SDK_INT < VERSION_CODES.M) { + // This is necessary to work around a bug that doesn't support themed color referenced in + // ColorStateList for API level < 23. + textAppearance.setTextColor( + MaterialResources.getColorStateList(context, a, R.styleable.Chip_android_textColor)); + } + setTextAppearance(textAppearance); int ellipsize = a.getInt(R.styleable.Chip_android_ellipsize, 0); @@ -1381,6 +1390,18 @@ public void setTextSize(@Dimension float size) { } } + public void setTextColor(@ColorInt int color) { + setTextColor(ColorStateList.valueOf(color)); + } + + public void setTextColor(@Nullable ColorStateList color) { + TextAppearance textAppearance = getTextAppearance(); + if (textAppearance != null) { + textAppearance.setTextColor(color); + invalidateSelf(); + } + } + /** Delegate interface to be implemented by Views that own a ChipDrawable. */ public interface Delegate { diff --git a/lib/java/com/google/android/material/chip/res/values/attrs.xml b/lib/java/com/google/android/material/chip/res/values/attrs.xml index dd4c6fc2ad1..da93147f7b5 100644 --- a/lib/java/com/google/android/material/chip/res/values/attrs.xml +++ b/lib/java/com/google/android/material/chip/res/values/attrs.xml @@ -47,10 +47,10 @@ - - + +