Skip to content

Commit

Permalink
[ChipDrawable] Support setting text color programmatically.
Browse files Browse the repository at this point in the history
Resolves #2526

GIT_ORIGIN_REV_ID=d1f47b5aa0b823ae0915d4c7ae85417e7d0eddd5
PiperOrigin-RevId: 424632069
  • Loading branch information
pubiqq authored and pekingme committed Jan 27, 2022
1 parent c66633b commit 10edcd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions lib/java/com/google/android/material/chip/ChipDrawable.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {

Expand Down
Expand Up @@ -47,10 +47,10 @@

<!-- Text to display on the chip. -->
<attr name="android:text"/>
<!-- Text color. -->
<attr name="android:textColor"/>
<!-- Default appearance of text: color, typeface, size, and style. -->
<attr name="android:textAppearance"/>
<!-- Text color. Overrides the color set in the textAppearance -->
<attr name="android:textColor"/>
<!-- Text size. Overrides the size set in the textAppearance -->
<attr name="android:textSize"/>
<!-- If set, causes words that are longer than the view is wide to be ellipsized instead of
Expand Down

0 comments on commit 10edcd1

Please sign in to comment.