Skip to content

Commit

Permalink
[TooltipDrawable] Adding android:textColor support
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 374887222
  • Loading branch information
drchen authored and pekingme committed May 20, 2021
1 parent f968731 commit 7adfd93
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private String createDescription(String name, @StyleRes int style) {
+ " - "
+ convertFontFamilyToDescription(textAppearance.fontFamily)
+ " "
+ pxToSp(textAppearance.textSize)
+ pxToSp(textAppearance.getTextSize())
+ "sp";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public SavedState(@NonNull Context context) {
// the TextAppearance.
TextAppearance textAppearance =
new TextAppearance(context, R.style.TextAppearance_MaterialComponents_Badge);
badgeTextColor = textAppearance.textColor.getDefaultColor();
badgeTextColor = textAppearance.getTextColor().getDefaultColor();
contentDescriptionNumberless =
context.getString(R.string.mtrl_badge_numberless_content_description);
contentDescriptionQuantityStrings = R.plurals.mtrl_badge_content_description;
Expand Down
17 changes: 9 additions & 8 deletions lib/java/com/google/android/material/chip/ChipDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ private void loadFromAttributes(
setText(a.getText(R.styleable.Chip_android_text));
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;
float textSize = a.getDimension(
R.styleable.Chip_android_textSize, textAppearance.getTextSize());
textAppearance.setTextSize(textSize);
setTextAppearance(textAppearance);

int ellipsize = a.getInt(R.styleable.Chip_android_ellipsize, 0);
Expand Down Expand Up @@ -1076,10 +1077,10 @@ private boolean onStateChange(@NonNull int[] chipState, @NonNull int[] closeIcon

int newTextColor =
textDrawableHelper.getTextAppearance() != null
&& textDrawableHelper.getTextAppearance().textColor != null
&& textDrawableHelper.getTextAppearance().getTextColor() != null
? textDrawableHelper
.getTextAppearance()
.textColor
.getTextAppearance()
.getTextColor()
.getColorForState(chipState, currentTextColor)
: 0;
if (currentTextColor != newTextColor) {
Expand Down Expand Up @@ -1147,8 +1148,8 @@ private static boolean isStateful(@Nullable Drawable drawable) {

private static boolean isStateful(@Nullable TextAppearance textAppearance) {
return textAppearance != null
&& textAppearance.textColor != null
&& textAppearance.textColor.isStateful();
&& textAppearance.getTextColor() != null
&& textAppearance.getTextColor().isStateful();
}

@Override
Expand Down Expand Up @@ -1374,7 +1375,7 @@ private static boolean hasState(@Nullable int[] stateSet, @AttrRes int state) {
public void setTextSize(@Dimension float size) {
TextAppearance textAppearance = getTextAppearance();
if (textAppearance != null) {
textAppearance.textSize = size;
textAppearance.setTextSize(size);
textDrawableHelper.getTextPaint().setTextSize(size);
onTextSizeChange();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@ public int getCollapsedTextGravity() {
public void setCollapsedTextAppearance(int resId) {
TextAppearance textAppearance = new TextAppearance(view.getContext(), resId);

if (textAppearance.textColor != null) {
collapsedTextColor = textAppearance.textColor;
if (textAppearance.getTextColor() != null) {
collapsedTextColor = textAppearance.getTextColor();
}
if (textAppearance.textSize != 0) {
collapsedTextSize = textAppearance.textSize;
if (textAppearance.getTextSize() != 0) {
collapsedTextSize = textAppearance.getTextSize();
}
if (textAppearance.shadowColor != null) {
collapsedShadowColor = textAppearance.shadowColor;
Expand Down Expand Up @@ -388,11 +388,11 @@ public void apply(Typeface font) {

public void setExpandedTextAppearance(int resId) {
TextAppearance textAppearance = new TextAppearance(view.getContext(), resId);
if (textAppearance.textColor != null) {
expandedTextColor = textAppearance.textColor;
if (textAppearance.getTextColor() != null) {
expandedTextColor = textAppearance.getTextColor();
}
if (textAppearance.textSize != 0) {
expandedTextSize = textAppearance.textSize;
if (textAppearance.getTextSize() != 0) {
expandedTextSize = textAppearance.getTextSize();
}
if (textAppearance.shadowColor != null) {
expandedShadowColor = textAppearance.shadowColor;
Expand Down
28 changes: 23 additions & 5 deletions lib/java/com/google/android/material/resources/TextAppearance.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class TextAppearance {
private static final int TYPEFACE_SERIF = 2;
private static final int TYPEFACE_MONOSPACE = 3;

@Nullable public final ColorStateList textColor;
@Nullable public final ColorStateList textColorHint;
@Nullable public final ColorStateList textColorLink;
@Nullable public final ColorStateList shadowColor;
Expand All @@ -69,7 +68,9 @@ public class TextAppearance {
public final boolean hasLetterSpacing;
public final float letterSpacing;

public float textSize;
@Nullable
private ColorStateList textColor;
private float textSize;

@FontRes private final int fontFamilyResourceId;

Expand All @@ -80,10 +81,10 @@ public class TextAppearance {
public TextAppearance(@NonNull Context context, @StyleRes int id) {
TypedArray a = context.obtainStyledAttributes(id, R.styleable.TextAppearance);

textSize = a.getDimension(R.styleable.TextAppearance_android_textSize, 0f);
textColor =
setTextSize(a.getDimension(R.styleable.TextAppearance_android_textSize, 0f));
setTextColor(
MaterialResources.getColorStateList(
context, a, R.styleable.TextAppearance_android_textColor);
context, a, R.styleable.TextAppearance_android_textColor));
textColorHint =
MaterialResources.getColorStateList(
context, a, R.styleable.TextAppearance_android_textColorHint);
Expand Down Expand Up @@ -353,6 +354,23 @@ public void updateTextPaintMeasureState(
}
}

@Nullable
public ColorStateList getTextColor() {
return textColor;
}

public void setTextColor(@Nullable ColorStateList textColor) {
this.textColor = textColor;
}

public float getTextSize() {
return textSize;
}

public void setTextSize(float textSize) {
this.textSize = textSize;
}

private boolean shouldLoadFontSynchronously(Context context) {
if (TextAppearanceConfig.shouldLoadFontSynchronously()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ private void loadFromAttributes(
getShapeAppearanceModel().toBuilder().setBottomEdge(createMarkerEdge()).build());

setText(a.getText(R.styleable.Tooltip_android_text));
setTextAppearance(
MaterialResources.getTextAppearance(
context, a, R.styleable.Tooltip_android_textAppearance));
TextAppearance textAppearance = MaterialResources.getTextAppearance(
context, a, R.styleable.Tooltip_android_textAppearance);
if (textAppearance != null && a.hasValue(R.styleable.Tooltip_android_textColor)) {
textAppearance.setTextColor(
MaterialResources.getColorStateList(context, a, R.styleable.Tooltip_android_textColor));
}
setTextAppearance(textAppearance);

int onBackground =
MaterialColors.getColor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<declare-styleable name="Tooltip">
<attr name="android:text" />
<attr name="android:textAppearance" />
<attr name="android:textColor" />
<attr name="android:layout_margin" />
<attr name="android:minWidth" />
<attr name="android:minHeight" />
Expand Down

0 comments on commit 7adfd93

Please sign in to comment.