diff --git a/api/current.txt b/api/current.txt
index 7f7474c539b02..5e4c0c1bab8a2 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -50517,11 +50517,9 @@ package android.widget {
method public void removeTextChangedListener(android.text.TextWatcher);
method public void setAllCaps(boolean);
method public final void setAutoLinkMask(int);
- method public void setAutoSizeMaxTextSize(int, float);
- method public void setAutoSizeMinTextSize(int, float);
- method public void setAutoSizeStepGranularity(int, float);
- method public void setAutoSizeTextPresetSizes(int[]);
- method public void setAutoSizeTextType(int);
+ method public void setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeUniformWithPresetSizes(int[], int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeWithDefaults(int);
method public void setBreakStrategy(int);
method public void setCompoundDrawablePadding(int);
method public void setCompoundDrawableTintList(android.content.res.ColorStateList);
diff --git a/api/system-current.txt b/api/system-current.txt
index 4aca489e49ab8..2e33d2d352a9e 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -54386,11 +54386,9 @@ package android.widget {
method public void removeTextChangedListener(android.text.TextWatcher);
method public void setAllCaps(boolean);
method public final void setAutoLinkMask(int);
- method public void setAutoSizeMaxTextSize(int, float);
- method public void setAutoSizeMinTextSize(int, float);
- method public void setAutoSizeStepGranularity(int, float);
- method public void setAutoSizeTextPresetSizes(int[]);
- method public void setAutoSizeTextType(int);
+ method public void setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeUniformWithPresetSizes(int[], int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeWithDefaults(int);
method public void setBreakStrategy(int);
method public void setCompoundDrawablePadding(int);
method public void setCompoundDrawableTintList(android.content.res.ColorStateList);
diff --git a/api/test-current.txt b/api/test-current.txt
index 9f7b8e903619f..c623eaf795657 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -50888,11 +50888,9 @@ package android.widget {
method public void removeTextChangedListener(android.text.TextWatcher);
method public void setAllCaps(boolean);
method public final void setAutoLinkMask(int);
- method public void setAutoSizeMaxTextSize(int, float);
- method public void setAutoSizeMinTextSize(int, float);
- method public void setAutoSizeStepGranularity(int, float);
- method public void setAutoSizeTextPresetSizes(int[]);
- method public void setAutoSizeTextType(int);
+ method public void setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeUniformWithPresetSizes(int[], int) throws java.lang.IllegalArgumentException;
+ method public void setAutoSizeTextTypeWithDefaults(int);
method public void setBreakStrategy(int);
method public void setCompoundDrawablePadding(int);
method public void setCompoundDrawableTintList(android.content.res.ColorStateList);
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 6fa84289d3ef9..051ffee677c20 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -113,6 +113,7 @@ import android.text.style.URLSpan;
import android.text.style.UpdateAppearance;
import android.text.util.Linkify;
import android.util.AttributeSet;
+import android.util.DisplayMetrics;
import android.util.IntArray;
import android.util.Log;
import android.util.TypedValue;
@@ -706,16 +707,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
private static final int DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP = 112;
// Default value for the step size in pixels.
private static final int DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX = 1;
+ // Use this to specify that any of the auto-size configuration int values have not been set.
+ private static final int UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE = -1;
// Auto-size text type.
private int mAutoSizeTextType = AUTO_SIZE_TEXT_TYPE_NONE;
// Specify if auto-size text is needed.
private boolean mNeedsAutoSizeText = false;
// Step size for auto-sizing in pixels.
- private int mAutoSizeStepGranularityInPx = 0;
+ private int mAutoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
// Minimum text size for auto-sizing in pixels.
- private int mAutoSizeMinTextSizeInPx = 0;
+ private int mAutoSizeMinTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
// Maximum text size for auto-sizing in pixels.
- private int mAutoSizeMaxTextSizeInPx = 0;
+ private int mAutoSizeMaxTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
// Contains a (specified or computed) distinct sorted set of text sizes in pixels to pick from
// when auto-sizing text.
private int[] mAutoSizeTextSizesInPx = EmptyArray.INT;
@@ -937,6 +940,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
CharSequence text = "";
CharSequence hint = null;
boolean password = false;
+ int autoSizeMinTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
+ int autoSizeMaxTextSizeInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
+ int autoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
int inputType = EditorInfo.TYPE_NULL;
a = theme.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
@@ -1304,15 +1310,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
break;
case com.android.internal.R.styleable.TextView_autoSizeStepGranularity:
- mAutoSizeStepGranularityInPx = a.getDimensionPixelSize(attr, 0);
+ autoSizeStepGranularityInPx = a.getDimensionPixelSize(attr,
+ UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE);
break;
case com.android.internal.R.styleable.TextView_autoSizeMinTextSize:
- mAutoSizeMinTextSizeInPx = a.getDimensionPixelSize(attr, 0);
+ autoSizeMinTextSizeInPx = a.getDimensionPixelSize(attr,
+ UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE);
break;
case com.android.internal.R.styleable.TextView_autoSizeMaxTextSize:
- mAutoSizeMaxTextSizeInPx = a.getDimensionPixelSize(attr, 0);
+ autoSizeMaxTextSizeInPx = a.getDimensionPixelSize(attr,
+ UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE);
break;
case com.android.internal.R.styleable.TextView_autoSizePresetSizes:
@@ -1320,7 +1329,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
if (autoSizeStepSizeArrayResId > 0) {
final TypedArray autoSizePreDefTextSizes = a.getResources()
.obtainTypedArray(autoSizeStepSizeArrayResId);
- setupAutoSizePresetSizes(autoSizePreDefTextSizes);
+ setupAutoSizeUniformPresetSizes(autoSizePreDefTextSizes);
autoSizePreDefTextSizes.recycle();
}
break;
@@ -1598,12 +1607,47 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
- setupAutoSizeText();
+ if (supportsAutoSizeText()) {
+ if (mAutoSizeTextType == AUTO_SIZE_TEXT_TYPE_UNIFORM) {
+ // If uniform auto-size has been specified but preset values have not been set then
+ // replace the auto-size configuration values that have not been specified with the
+ // defaults.
+ if (!mHasPresetAutoSizeValues) {
+ final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
+
+ if (autoSizeMinTextSizeInPx == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
+ autoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_SP,
+ DEFAULT_AUTO_SIZE_MIN_TEXT_SIZE_IN_SP,
+ displayMetrics);
+ }
+
+ if (autoSizeMaxTextSizeInPx == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
+ autoSizeMaxTextSizeInPx = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_SP,
+ DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP,
+ displayMetrics);
+ }
+
+ if (autoSizeMinTextSizeInPx == UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE) {
+ autoSizeStepGranularityInPx = DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX;
+ }
+
+ validateAndSetAutoSizeTextTypeUniformConfiguration(autoSizeMinTextSizeInPx,
+ autoSizeMaxTextSizeInPx,
+ autoSizeStepGranularityInPx);
+ }
+
+ setupAutoSizeText();
+ }
+ } else {
+ mAutoSizeTextType = AUTO_SIZE_TEXT_TYPE_NONE;
+ }
}
/**
* Specify whether this widget should automatically scale the text to try to perfectly fit
- * within the layout bounds by taking into account the auto-size configuration.
+ * within the layout bounds by using the default auto-size configuration.
*
* @param autoSizeTextType the type of auto-size. Must be one of
* {@link TextView#AUTO_SIZE_TEXT_TYPE_NONE} or
@@ -1613,25 +1657,28 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*
* @see #getAutoSizeTextType()
*/
- public void setAutoSizeTextType(@AutoSizeTextType int autoSizeTextType) {
+ public void setAutoSizeTextTypeWithDefaults(@AutoSizeTextType int autoSizeTextType) {
if (supportsAutoSizeText()) {
switch (autoSizeTextType) {
case AUTO_SIZE_TEXT_TYPE_NONE:
- if (mAutoSizeTextType != AUTO_SIZE_TEXT_TYPE_NONE) {
- // Clear all auto-size configuration
- mAutoSizeTextType = AUTO_SIZE_TEXT_TYPE_NONE;
- mAutoSizeMinTextSizeInPx = 0;
- mAutoSizeMaxTextSizeInPx = 0;
- mAutoSizeStepGranularityInPx = 0;
- mAutoSizeTextSizesInPx = EmptyArray.INT;
- mNeedsAutoSizeText = false;
- }
+ clearAutoSizeConfiguration();
break;
case AUTO_SIZE_TEXT_TYPE_UNIFORM:
- if (mAutoSizeTextType != AUTO_SIZE_TEXT_TYPE_UNIFORM) {
- mAutoSizeTextType = AUTO_SIZE_TEXT_TYPE_UNIFORM;
- setupAutoSizeText();
- }
+ final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
+ final int autoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_SP,
+ DEFAULT_AUTO_SIZE_MIN_TEXT_SIZE_IN_SP,
+ displayMetrics);
+ final int autoSizeMaxTextSizeInPx = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_SP,
+ DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP,
+ displayMetrics);
+
+ validateAndSetAutoSizeTextTypeUniformConfiguration(
+ autoSizeMinTextSizeInPx,
+ autoSizeMaxTextSizeInPx,
+ DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX);
+ setupAutoSizeText();
break;
default:
throw new IllegalArgumentException(
@@ -1640,6 +1687,111 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
}
+ /**
+ * Specify whether this widget should automatically scale the text to try to perfectly fit
+ * within the layout bounds. If all the configuration params are valid the type of auto-size is
+ * set to {@link #AUTO_SIZE_TEXT_TYPE_UNIFORM}.
+ *
+ * @param autoSizeMinTextSize the minimum text size available for auto-size
+ * @param autoSizeMaxTextSize the maximum text size available for auto-size
+ * @param autoSizeStepGranularity the auto-size step granularity. It is used in conjunction with
+ * the minimum and maximum text size in order to build the set of
+ * text sizes the system uses to choose from when auto-sizing
+ * @param unit the desired dimension unit for all sizes above. See {@link TypedValue} for the
+ * possible dimension units
+ *
+ * @throws IllegalArgumentException if any of the configuration params are invalid.
+ *
+ * @attr ref android.R.styleable#TextView_autoSizeText
+ * @attr ref android.R.styleable#TextView_autoSizeMinTextSize
+ * @attr ref android.R.styleable#TextView_autoSizeMaxTextSize
+ * @attr ref android.R.styleable#TextView_autoSizeStepGranularity
+ *
+ * @see #setAutoSizeTextTypeWithDefaults(int)
+ * @see #setAutoSizeTextTypeUniformWithPresetSizes(int[], int)
+ * @see #getAutoSizeMinTextSize()
+ * @see #getAutoSizeMaxTextSize()
+ * @see #getAutoSizeStepGranularity()
+ * @see #getAutoSizeTextAvailableSizes()
+ */
+ public void setAutoSizeTextTypeUniformWithConfiguration(
+ int autoSizeMinTextSize,
+ int autoSizeMaxTextSize,
+ int autoSizeStepGranularity,
+ int unit) throws IllegalArgumentException {
+ if (supportsAutoSizeText()) {
+ final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
+ final int autoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
+ unit, autoSizeMinTextSize, displayMetrics);
+ final int autoSizeMaxTextSizeInPx = (int) TypedValue.applyDimension(
+ unit, autoSizeMaxTextSize, displayMetrics);
+ final int autoSizeStepGranularityInPx = (int) TypedValue.applyDimension(
+ unit, autoSizeStepGranularity, displayMetrics);
+
+ validateAndSetAutoSizeTextTypeUniformConfiguration(autoSizeMinTextSizeInPx,
+ autoSizeMaxTextSizeInPx,
+ autoSizeStepGranularityInPx);
+ setupAutoSizeText();
+ }
+ }
+
+ /**
+ * Specify whether this widget should automatically scale the text to try to perfectly fit
+ * within the layout bounds. If at least one value from the presetSizes is valid
+ * then the type of auto-size is set to {@link #AUTO_SIZE_TEXT_TYPE_UNIFORM}.
+ *
+ * @param presetSizes an {@code int} array of sizes in pixels
+ * @param unit the desired dimension unit for the preset sizes above. See {@link TypedValue} for
+ * the possible dimension units
+ *
+ * @throws IllegalArgumentException if all of the presetSizes are invalid.
+ *
+ * @attr ref android.R.styleable#TextView_autoSizeText
+ * @attr ref android.R.styleable#TextView_autoSizePresetSizes
+ *
+ * @see #setAutoSizeTextTypeWithDefaults(int)
+ * @see #setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)
+ * @see #getAutoSizeMinTextSize()
+ * @see #getAutoSizeMaxTextSize()
+ * @see #getAutoSizeTextAvailableSizes()
+ */
+ public void setAutoSizeTextTypeUniformWithPresetSizes(@NonNull int[] presetSizes, int unit)
+ throws IllegalArgumentException {
+ if (supportsAutoSizeText()) {
+ final int presetSizesLength = presetSizes.length;
+ if (presetSizesLength > 0) {
+ int[] presetSizesInPx = new int[presetSizesLength];
+
+ if (unit == TypedValue.COMPLEX_UNIT_PX) {
+ presetSizesInPx = Arrays.copyOf(presetSizes, presetSizesLength);
+ } else {
+ final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
+ // Convert all to sizes to pixels.
+ for (int i = 0; i < presetSizesLength; i++) {
+ presetSizesInPx[i] = (int) TypedValue.applyDimension(unit, presetSizes[i],
+ displayMetrics);
+ }
+ }
+
+ mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(presetSizesInPx);
+ final int sizesLength = mAutoSizeTextSizesInPx.length;
+ mHasPresetAutoSizeValues = sizesLength > 0;
+ if (mHasPresetAutoSizeValues) {
+ mAutoSizeTextType = AUTO_SIZE_TEXT_TYPE_UNIFORM;
+ mAutoSizeMinTextSizeInPx = mAutoSizeTextSizesInPx[0];
+ mAutoSizeMaxTextSizeInPx = mAutoSizeTextSizesInPx[sizesLength - 1];
+ mAutoSizeStepGranularityInPx = UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE;
+ } else {
+ throw new IllegalArgumentException("None of the preset sizes is valid: "
+ + Arrays.toString(presetSizes));
+ }
+ } else {
+ mHasPresetAutoSizeValues = false;
+ }
+ setupAutoSizeText();
+ }
+ }
+
/**
* Returns the type of auto-size set for this widget.
*
@@ -1649,7 +1801,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*
* @attr ref android.R.styleable#TextView_autoSizeText
*
- * @see #setAutoSizeTextType(int)
+ * @see #setAutoSizeTextTypeWithDefaults(int)
+ * @see #setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)
+ * @see #setAutoSizeTextTypeUniformWithPresetSizes(int[], int)
*/
@AutoSizeTextType
public int getAutoSizeTextType() {
@@ -1657,155 +1811,53 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
/**
- * Sets the auto-size step granularity. It is used in conjunction with auto-size minimum
- * and maximum text size in order to build the set of text sizes the system uses to choose
- * from when auto-sizing.
- *
- * @param unit the desired dimension unit. See {@link TypedValue} for the possible
- * dimension units
- * @param size the desired size in the given units
+ * @return the current auto-size step granularity in pixels.
*
* @attr ref android.R.styleable#TextView_autoSizeStepGranularity
*
- * @see #getAutoSizeStepGranularity()
- * @see #setAutoSizeMinTextSize(int, float)
- * @see #setAutoSizeMaxTextSize(int, float)
- */
- public void setAutoSizeStepGranularity(int unit, float size) {
- if (supportsAutoSizeText()) {
- mAutoSizeStepGranularityInPx = (int) TypedValue.applyDimension(
- unit, size, getResources().getDisplayMetrics());
- mHasPresetAutoSizeValues = false;
- setupAutoSizeText();
- }
- }
-
- /**
- * @return the current auto-size step granularity in pixels.
- *
- * @see #setAutoSizeStepGranularity(int, float)
+ * @see #setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)
*/
public int getAutoSizeStepGranularity() {
return mAutoSizeStepGranularityInPx;
}
/**
- * Sets the minimum text size to be used in conjunction with auto-size maximum text size and
- * auto-size step granularity in order to build the set of text sizes the system uses to choose
- * from when auto-sizing.
- *
- * @param unit the desired dimension unit. See {@link TypedValue} for the possible
- * dimension units
- * @param size the desired size in the given units
+ * @return the current auto-size minimum text size in pixels (the default is 12sp). Note that
+ * if auto-size has not been configured this function returns {@code -1}.
*
* @attr ref android.R.styleable#TextView_autoSizeMinTextSize
*
- * @see #getAutoSizeMinTextSize()
- * @see #setAutoSizeMaxTextSize(int, float)
- * @see #setAutoSizeStepGranularity(int, float)
- */
- public void setAutoSizeMinTextSize(int unit, float size) {
- if (supportsAutoSizeText()) {
- mAutoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
- unit, size, getResources().getDisplayMetrics());
- mHasPresetAutoSizeValues = false;
- setupAutoSizeText();
- }
- }
-
- /**
- * @return the current auto-size minimum text size in pixels (the default is 12sp). Note that
- * if auto-size has not been configured this function returns {@code 0}.
- *
- * @see #setAutoSizeMinTextSize(int, float)
+ * @see #setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)
+ * @see #setAutoSizeTextTypeUniformWithPresetSizes(int[], int)
*/
public int getAutoSizeMinTextSize() {
return mAutoSizeMinTextSizeInPx;
}
/**
- * Sets the maximum text size to be used in conjunction with auto-size minimum text size and
- * auto-size step granularity in order to build the set of text sizes the system uses to choose
- * from when auto-sizing.
- *
- * @param unit the desired dimension unit. See {@link TypedValue} for the possible
- * dimension units
- * @param size the desired size in the given units
+ * @return the current auto-size maximum text size in pixels (the default is 112sp). Note that
+ * if auto-size has not been configured this function returns {@code -1}.
*
* @attr ref android.R.styleable#TextView_autoSizeMaxTextSize
*
- * @see #getAutoSizeMaxTextSize()
- * @see #setAutoSizeMinTextSize(int, float)
- * @see #setAutoSizeStepGranularity(int, float)
- */
- public void setAutoSizeMaxTextSize(int unit, float size) {
- if (supportsAutoSizeText()) {
- mAutoSizeMaxTextSizeInPx = (int) TypedValue.applyDimension(
- unit, size, getResources().getDisplayMetrics());
- mHasPresetAutoSizeValues = false;
- setupAutoSizeText();
- }
- }
-
- /**
- * @return the current auto-size maximum text size in pixels (the default is 112sp). Note that
- * if auto-size has not been configured this function returns {@code 0}.
- *
- * @see #setAutoSizeMaxTextSize(int, float)
+ * @see #setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)
+ * @see #setAutoSizeTextTypeUniformWithPresetSizes(int[], int)
*/
public int getAutoSizeMaxTextSize() {
return mAutoSizeMaxTextSizeInPx;
}
- /**
- * Sets a predefined array of sizes to be used when auto-sizing.
- *
- *
presetSizes is not empty then the auto-size algorithm will use the
- * values provided here instead of calculating the values based on min, max and step size. Also
- * the values will be de-duplicated, sorted and negative or zero values will be removed.
- * presetSizes is empty then the auto-size algorithm will use the min, max
- * and step size to build the set of available sizes to choose from. Note that if no values have
- * been provided for any of min, max or step size then defaults will be used.
- *