Skip to content

Commit

Permalink
[Chip] Support setting text size without changing TextAppearance
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 320083914
  • Loading branch information
ymarian committed Jul 8, 2020
1 parent 272ce50 commit e5f7951
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
17 changes: 15 additions & 2 deletions lib/java/com/google/android/material/chip/ChipDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.Dimension;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -352,8 +353,11 @@ private void loadFromAttributes(
setRippleColor(MaterialResources.getColorStateList(context, a, R.styleable.Chip_rippleColor));

setText(a.getText(R.styleable.Chip_android_text));
setTextAppearance(
MaterialResources.getTextAppearance(context, a, R.styleable.Chip_android_textAppearance));
TextAppearance textAppearance =
MaterialResources.getTextAppearance(context, a, R.styleable.Chip_android_textAppearance);
float textSize = a.getDimension(R.styleable.Chip_android_textSize, textAppearance.textSize);
textAppearance.textSize = textSize;
setTextAppearance(textAppearance);

int ellipsize = a.getInt(R.styleable.Chip_android_ellipsize, 0);
// Convert to supported TextUtils.TruncateAt values
Expand Down Expand Up @@ -1365,6 +1369,15 @@ private static boolean hasState(@Nullable int[] stateSet, @AttrRes int state) {
return false;
}

public void setTextSize(@Dimension float size) {
TextAppearance textAppearance = getTextAppearance();
if (textAppearance != null) {
textAppearance.textSize = size;
textDrawableHelper.getTextPaint().setTextSize(size);
onTextSizeChange();
}
}

/** Delegate interface to be implemented by Views that own a ChipDrawable. */
public interface Delegate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<attr name="android:textColor"/>
<!-- Default appearance of text: color, typeface, size, and style. -->
<attr name="android:textAppearance"/>
<!-- 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
truncated at the end. -->
<attr name="android:ellipsize"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ public class TextAppearance {
private static final int TYPEFACE_SERIF = 2;
private static final int TYPEFACE_MONOSPACE = 3;

public final float textSize;
@Nullable public final ColorStateList textColor;
@Nullable public final ColorStateList textColorHint;
@Nullable public final ColorStateList textColorLink;
@Nullable public final ColorStateList shadowColor;
@Nullable public final String fontFamily;

public final int textStyle;
public final int typeface;
@Nullable public final String fontFamily;
public final boolean textAllCaps;
@Nullable public final ColorStateList shadowColor;
public final float shadowDx;
public final float shadowDy;
public final float shadowRadius;

public float textSize;

@FontRes private final int fontFamilyResourceId;

private boolean fontResolved = false;
Expand Down

0 comments on commit e5f7951

Please sign in to comment.