Skip to content

Commit 10edcd1

Browse files
pubiqqpekingme
authored andcommitted
[ChipDrawable] Support setting text color programmatically.
Resolves #2526 GIT_ORIGIN_REV_ID=d1f47b5aa0b823ae0915d4c7ae85417e7d0eddd5 PiperOrigin-RevId: 424632069
1 parent c66633b commit 10edcd1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/java/com/google/android/material/chip/ChipDrawable.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import android.graphics.drawable.RippleDrawable;
4343
import android.graphics.drawable.ShapeDrawable;
4444
import android.graphics.drawable.shapes.OvalShape;
45+
import android.os.Build.VERSION;
4546
import android.os.Build.VERSION_CODES;
4647
import androidx.appcompat.content.res.AppCompatResources;
4748
import android.text.TextUtils;
@@ -358,6 +359,14 @@ private void loadFromAttributes(
358359
float textSize = a.getDimension(
359360
R.styleable.Chip_android_textSize, textAppearance.getTextSize());
360361
textAppearance.setTextSize(textSize);
362+
363+
if (VERSION.SDK_INT < VERSION_CODES.M) {
364+
// This is necessary to work around a bug that doesn't support themed color referenced in
365+
// ColorStateList for API level < 23.
366+
textAppearance.setTextColor(
367+
MaterialResources.getColorStateList(context, a, R.styleable.Chip_android_textColor));
368+
}
369+
361370
setTextAppearance(textAppearance);
362371

363372
int ellipsize = a.getInt(R.styleable.Chip_android_ellipsize, 0);
@@ -1381,6 +1390,18 @@ public void setTextSize(@Dimension float size) {
13811390
}
13821391
}
13831392

1393+
public void setTextColor(@ColorInt int color) {
1394+
setTextColor(ColorStateList.valueOf(color));
1395+
}
1396+
1397+
public void setTextColor(@Nullable ColorStateList color) {
1398+
TextAppearance textAppearance = getTextAppearance();
1399+
if (textAppearance != null) {
1400+
textAppearance.setTextColor(color);
1401+
invalidateSelf();
1402+
}
1403+
}
1404+
13841405
/** Delegate interface to be implemented by Views that own a ChipDrawable. */
13851406
public interface Delegate {
13861407

lib/java/com/google/android/material/chip/res/values/attrs.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747

4848
<!-- Text to display on the chip. -->
4949
<attr name="android:text"/>
50-
<!-- Text color. -->
51-
<attr name="android:textColor"/>
5250
<!-- Default appearance of text: color, typeface, size, and style. -->
5351
<attr name="android:textAppearance"/>
52+
<!-- Text color. Overrides the color set in the textAppearance -->
53+
<attr name="android:textColor"/>
5454
<!-- Text size. Overrides the size set in the textAppearance -->
5555
<attr name="android:textSize"/>
5656
<!-- If set, causes words that are longer than the view is wide to be ellipsized instead of

0 commit comments

Comments
 (0)