From 7adfd93d803e2797143ece017d67d7dee0392710 Mon Sep 17 00:00:00 2001 From: conradchen Date: Thu, 20 May 2021 09:26:24 -0700 Subject: [PATCH] [TooltipDrawable] Adding android:textColor support PiperOrigin-RevId: 374887222 --- .../catalog/font/FontMainDemoFragment.java | 2 +- .../android/material/badge/BadgeDrawable.java | 2 +- .../android/material/chip/ChipDrawable.java | 17 +++++------ .../internal/CollapsingTextHelper.java | 16 +++++------ .../material/resources/TextAppearance.java | 28 +++++++++++++++---- .../material/tooltip/TooltipDrawable.java | 10 +++++-- .../material/tooltip/res/values/attrs.xml | 1 + 7 files changed, 50 insertions(+), 26 deletions(-) diff --git a/catalog/java/io/material/catalog/font/FontMainDemoFragment.java b/catalog/java/io/material/catalog/font/FontMainDemoFragment.java index 5fe46695320..32d1e914b94 100644 --- a/catalog/java/io/material/catalog/font/FontMainDemoFragment.java +++ b/catalog/java/io/material/catalog/font/FontMainDemoFragment.java @@ -195,7 +195,7 @@ private String createDescription(String name, @StyleRes int style) { + " - " + convertFontFamilyToDescription(textAppearance.fontFamily) + " " - + pxToSp(textAppearance.textSize) + + pxToSp(textAppearance.getTextSize()) + "sp"; } diff --git a/lib/java/com/google/android/material/badge/BadgeDrawable.java b/lib/java/com/google/android/material/badge/BadgeDrawable.java index 00fff9bad3a..6239a1ba356 100644 --- a/lib/java/com/google/android/material/badge/BadgeDrawable.java +++ b/lib/java/com/google/android/material/badge/BadgeDrawable.java @@ -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; diff --git a/lib/java/com/google/android/material/chip/ChipDrawable.java b/lib/java/com/google/android/material/chip/ChipDrawable.java index 683c55b2a49..3a8c46584fb 100644 --- a/lib/java/com/google/android/material/chip/ChipDrawable.java +++ b/lib/java/com/google/android/material/chip/ChipDrawable.java @@ -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); @@ -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) { @@ -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 @@ -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(); } diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java index df240962a84..815100c6f94 100644 --- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java +++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java @@ -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; @@ -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; diff --git a/lib/java/com/google/android/material/resources/TextAppearance.java b/lib/java/com/google/android/material/resources/TextAppearance.java index 0cbb1b6b5da..4be836b7db3 100644 --- a/lib/java/com/google/android/material/resources/TextAppearance.java +++ b/lib/java/com/google/android/material/resources/TextAppearance.java @@ -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; @@ -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; @@ -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); @@ -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; diff --git a/lib/java/com/google/android/material/tooltip/TooltipDrawable.java b/lib/java/com/google/android/material/tooltip/TooltipDrawable.java index c2a7f83845e..055f00a9e57 100644 --- a/lib/java/com/google/android/material/tooltip/TooltipDrawable.java +++ b/lib/java/com/google/android/material/tooltip/TooltipDrawable.java @@ -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( diff --git a/lib/java/com/google/android/material/tooltip/res/values/attrs.xml b/lib/java/com/google/android/material/tooltip/res/values/attrs.xml index 8413eff97f0..41a85e405ad 100644 --- a/lib/java/com/google/android/material/tooltip/res/values/attrs.xml +++ b/lib/java/com/google/android/material/tooltip/res/values/attrs.xml @@ -18,6 +18,7 @@ +