TextViewCompat
public final class TextViewCompat
Helper for accessing features in TextView.
Summary
Constants |
|
|---|---|
static final int |
The TextView does not auto-size text (default). |
static final int |
The TextView scales text size both horizontally and vertically to fit within the container. |
Public methods |
|
|---|---|
static int |
getAutoSizeMaxTextSize(@NonNull TextView textView)name android:autoSizeMaxTextSize |
static int |
getAutoSizeMinTextSize(@NonNull TextView textView)name android:autoSizeMinTextSize |
static int |
getAutoSizeStepGranularity(@NonNull TextView textView)name android:autoSizeStepGranularity |
static @NonNull int[] |
getAutoSizeTextAvailableSizes(@NonNull TextView textView)name android:autoSizePresetSizes |
static int |
getAutoSizeTextType(@NonNull TextView textView)Returns the type of auto-size set for this widget. |
static @Nullable ColorStateList |
getCompoundDrawableTintList(@NonNull TextView textView)Return the tint applied to any compound drawables. |
static @Nullable PorterDuff.Mode |
getCompoundDrawableTintMode(@NonNull TextView textView)Return the tint mode applied to any compound drawables. |
static @NonNull Drawable[] |
@ReplaceWith(expression = "textView.getCompoundDrawablesRelative()")This method is deprecated. Call |
static int |
getFirstBaselineToTopHeight(@NonNull TextView textView)Returns the distance between the first text baseline and the top of this TextView. |
static int |
getLastBaselineToBottomHeight(@NonNull TextView textView)Returns the distance between the last text baseline and the bottom of this TextView. |
static int |
@ReplaceWith(expression = "textView.getMaxLines()")This method is deprecated. Call |
static int |
@ReplaceWith(expression = "textView.getMinLines()")This method is deprecated. Call |
static @NonNull PrecomputedTextCompat.Params |
getTextMetricsParams(@NonNull TextView textView)Gets the parameters for text layout precomputation, for use with |
static void |
setAutoSizeTextTypeUniformWithConfiguration(Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds. |
static void |
setAutoSizeTextTypeUniformWithPresetSizes(Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds. |
static void |
setAutoSizeTextTypeWithDefaults(Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds by using the default auto-size configuration. |
static void |
setCompoundDrawableTintList(Applies a tint to any compound drawables. |
static void |
setCompoundDrawableTintMode(Applies a tint mode to any compound drawables. |
static void |
@ReplaceWith(expression = "textView.setCompoundDrawablesRelative(start, top, end, bottom)")This method is deprecated. Call setCompoundDrawablesRelative directly. |
static void |
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")This method is deprecated. Call setCompoundDrawablesRelativeWithIntrinsicBounds directly. |
static void |
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")This method is deprecated. Call setCompoundDrawablesRelativeWithIntrinsicBounds directly. |
static void |
@ReplaceWith(expression = "textView.setCustomSelectionActionModeCallback(callback)")This method is deprecated. Call |
static void |
setFirstBaselineToTopHeight(Updates the top padding of the TextView so that |
static void |
setLastBaselineToBottomHeight(Updates the bottom padding of the TextView so that |
static void |
setLineHeight(Sets an explicit line height for this TextView. |
static void |
setLineHeight(Sets an explicit line height to a given unit and value for the TextView. |
static void |
setPrecomputedText(Sets the PrecomputedTextCompat to the TextView If the given PrecomputeTextCompat is not compatible with textView, throws an IllegalArgumentException. |
static void |
setTextAppearance(@NonNull TextView textView, @StyleRes int resId)Sets the text appearance from the specified style resource. |
static void |
setTextMetricsParams(Apply the text layout parameter. |
Constants
AUTO_SIZE_TEXT_TYPE_NONE
public static final int AUTO_SIZE_TEXT_TYPE_NONE = 0
The TextView does not auto-size text (default).
AUTO_SIZE_TEXT_TYPE_UNIFORM
public static final int AUTO_SIZE_TEXT_TYPE_UNIFORM = 1
The TextView scales text size both horizontally and vertically to fit within the container.
Public methods
getAutoSizeMaxTextSize
public static int getAutoSizeMaxTextSize(@NonNull TextView textView)
name android:autoSizeMaxTextSize
| Returns | |
|---|---|
int |
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 |
getAutoSizeMinTextSize
public static int getAutoSizeMinTextSize(@NonNull TextView textView)
name android:autoSizeMinTextSize
| Returns | |
|---|---|
int |
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 |
getAutoSizeStepGranularity
public static int getAutoSizeStepGranularity(@NonNull TextView textView)
name android:autoSizeStepGranularity
| Returns | |
|---|---|
int |
the current auto-size step granularity in pixels. |
getAutoSizeTextAvailableSizes
public static @NonNull int[] getAutoSizeTextAvailableSizes(@NonNull TextView textView)
name android:autoSizePresetSizes
| Returns | |
|---|---|
@NonNull int[] |
the current auto-size |
getAutoSizeTextType
public static int getAutoSizeTextType(@NonNull TextView textView)
Returns the type of auto-size set for this widget.
name android:autoSizeTextType
| Returns | |
|---|---|
int |
an |
getCompoundDrawableTintList
public static @Nullable ColorStateList getCompoundDrawableTintList(@NonNull TextView textView)
Return the tint applied to any compound drawables.
Only returns meaningful info when running on API v24 or newer, or if textView implements the TintableCompoundDrawablesView interface.
getCompoundDrawableTintMode
public static @Nullable PorterDuff.Mode getCompoundDrawableTintMode(@NonNull TextView textView)
Return the tint mode applied to any compound drawables.
Only returns meaningful info when running on API v24 or newer, or if textView implements the TintableCompoundDrawablesView interface.
@ReplaceWith(expression = "textView.getCompoundDrawablesRelative()")
public static @NonNull Drawable[]getCompoundDrawablesRelative(@NonNull TextView textView)
Returns drawables for the start, top, end, and bottom borders from the given text view.
getFirstBaselineToTopHeight
public static int getFirstBaselineToTopHeight(@NonNull TextView textView)
Returns the distance between the first text baseline and the top of this TextView.
name android:firstBaselineToTopHeight
| See also | |
|---|---|
setFirstBaselineToTopHeight |
getLastBaselineToBottomHeight
public static int getLastBaselineToBottomHeight(@NonNull TextView textView)
Returns the distance between the last text baseline and the bottom of this TextView.
name android:lastBaselineToBottomHeight
| See also | |
|---|---|
setLastBaselineToBottomHeight |
@ReplaceWith(expression = "textView.getMaxLines()")
public static intgetMaxLines(@NonNull TextView textView)
Returns the maximum number of lines displayed in the given TextView, or -1 if the maximum height was set in pixels instead.
@ReplaceWith(expression = "textView.getMinLines()")
public static intgetMinLines(@NonNull TextView textView)
Returns the minimum number of lines displayed in the given TextView, or -1 if the minimum height was set in pixels instead.
getTextMetricsParams
public static @NonNull PrecomputedTextCompat.Params getTextMetricsParams(@NonNull TextView textView)
Gets the parameters for text layout precomputation, for use with PrecomputedTextCompat.
| Returns | |
|---|---|
@NonNull PrecomputedTextCompat.Params |
a current |
| See also | |
|---|---|
PrecomputedTextCompat |
setAutoSizeTextTypeUniformWithConfiguration
public static void setAutoSizeTextTypeUniformWithConfiguration(
@NonNull TextView textView,
int autoSizeMinTextSize,
int autoSizeMaxTextSize,
int autoSizeStepGranularity,
int unit
)
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 AUTO_SIZE_TEXT_TYPE_UNIFORM.
name android:autoSizeTextType
name android:autoSizeTextType
name android:autoSizeMinTextSize
name android:autoSizeMaxTextSize
name android:autoSizeStepGranularity
| Parameters | |
|---|---|
@NonNull TextView textView |
TextView for which to set the mode. |
int autoSizeMinTextSize |
the minimum text size available for auto-size |
int autoSizeMaxTextSize |
the maximum text size available for auto-size |
int 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 |
int unit |
the desired dimension unit for all sizes above. See |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if any of the configuration params are invalid. |
setAutoSizeTextTypeUniformWithPresetSizes
public static void setAutoSizeTextTypeUniformWithPresetSizes(
@NonNull TextView textView,
@NonNull int[] presetSizes,
int unit
)
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 AUTO_SIZE_TEXT_TYPE_UNIFORM.
name android:autoSizeTextType
name android:autoSizePresetSizes
| Parameters | |
|---|---|
@NonNull TextView textView |
TextView for which to set the mode. |
@NonNull int[] presetSizes |
an |
int unit |
the desired dimension unit for the preset sizes above. See |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if all of the |
setAutoSizeTextTypeWithDefaults
public static void setAutoSizeTextTypeWithDefaults(
@NonNull TextView textView,
int autoSizeTextType
)
Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds by using the default auto-size configuration.
name android:autoSizeTextType
| Parameters | |
|---|---|
@NonNull TextView textView |
TextView for which to set the mode. |
int autoSizeTextType |
the type of auto-size. Must be one of |
setCompoundDrawableTintList
public static void setCompoundDrawableTintList(
@NonNull TextView textView,
@Nullable ColorStateList tint
)
Applies a tint to any compound drawables.
This will always take effect when running on API v24 or newer. When running on platforms previous to API v24, it will only take effect if textView implements the TintableCompoundDrawablesView interface.
setCompoundDrawableTintMode
public static void setCompoundDrawableTintMode(
@NonNull TextView textView,
@Nullable PorterDuff.Mode tintMode
)
Applies a tint mode to any compound drawables.
This will always take effect when running on API v24 or newer. When running on platforms previous to API v24, it will only take effect if textView implements the TintableCompoundDrawablesView interface.
@ReplaceWith(expression = "textView.setCompoundDrawablesRelative(start, top, end, bottom)")
public static voidsetCompoundDrawablesRelative(
@NonNull TextView textView,
@Nullable Drawable start,
@Nullable Drawable top,
@Nullable Drawable end,
@Nullable Drawable bottom
)
Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds called.
setCompoundDrawables or related methods.
name android:drawableStart
name android:drawableTop
name android:drawableEnd
name android:drawableBottom
| Parameters | |
|---|---|
@NonNull TextView textView |
The TextView against which to invoke the method. |
@Nullable Drawable start |
position in pixels of the start bound |
@Nullable Drawable top |
position in pixels of the top bound |
@Nullable Drawable end |
position in pixels of the end bound |
@Nullable Drawable bottom |
position in pixels of the bottom bound |
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
public static voidsetCompoundDrawablesRelativeWithIntrinsicBounds(
@NonNull TextView textView,
@Nullable Drawable start,
@Nullable Drawable top,
@Nullable Drawable end,
@Nullable Drawable bottom
)
Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
setCompoundDrawables or related methods.
name android:drawableStart
name android:drawableTop
name android:drawableEnd
name android:drawableBottom
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
public static voidsetCompoundDrawablesRelativeWithIntrinsicBounds(
@NonNull TextView textView,
@DrawableRes int start,
@DrawableRes int top,
@DrawableRes int end,
@DrawableRes int bottom
)
Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
Calling this method will overwrite any Drawables previously set usingsetCompoundDrawables or related methods.
name android:drawableStart
name android:drawableTop
name android:drawableEnd
name android:drawableBottom
| Parameters | |
|---|---|
@NonNull TextView textView |
The TextView against which to invoke the method. |
@DrawableRes int start |
Resource identifier of the start Drawable. |
@DrawableRes int top |
Resource identifier of the top Drawable. |
@DrawableRes int end |
Resource identifier of the end Drawable. |
@DrawableRes int bottom |
Resource identifier of the bottom Drawable. |
@ReplaceWith(expression = "textView.setCustomSelectionActionModeCallback(callback)")
public static voidsetCustomSelectionActionModeCallback(
@NonNull TextView textView,
@NonNull ActionMode.Callback callback
)
Sets a selection action mode callback on a TextView. Also this method can be used to fix a bug in framework SDK 26/27. On these affected devices, the bug causes the menu containing the options for handling ACTION_PROCESS_TEXT after text selection to miss a number of items. This method can be used to fix this wrong behaviour for a text view, by passing any custom callback implementation. If no custom callback is desired, a no-op implementation should be provided. Note that, by default, the bug will only be fixed when the default floating toolbar menu implementation is used. If a custom implementation of Menu is provided, this should provide the method Menu#removeItemAt(int) which removes a menu item by its position, as given by Menu#getItem(int). Also, the following post condition should hold: a call to removeItemAt(i), should not modify the results of getItem(j) for any j
| Parameters | |
|---|---|
@NonNull TextView textView |
The TextView to set the action selection mode callback on. |
@NonNull ActionMode.Callback callback |
The action selection mode callback to set on textView. |
setFirstBaselineToTopHeight
public static void setFirstBaselineToTopHeight(
@NonNull TextView textView,
@Px @IntRange(from = 0) int firstBaselineToTopHeight
)
Updates the top padding of the TextView so that firstBaselineToTopHeight is equal to the distance between the first text baseline and the top of this TextView. Note that if FontMetrics.top or FontMetrics.ascent was already greater than firstBaselineToTopHeight, the top padding is not updated.
name android:firstBaselineToTopHeight
setLastBaselineToBottomHeight
public static void setLastBaselineToBottomHeight(
@NonNull TextView textView,
@Px @IntRange(from = 0) int lastBaselineToBottomHeight
)
Updates the bottom padding of the TextView so that lastBaselineToBottomHeight is equal to the distance between the last text baseline and the bottom of this TextView. Note that if FontMetrics.bottom or FontMetrics.descent was already greater than lastBaselineToBottomHeight, the bottom padding is not updated.
name android:lastBaselineToBottomHeight
setLineHeight
public static void setLineHeight(
@NonNull TextView textView,
@Px @IntRange(from = 0) int lineHeight
)
Sets an explicit line height for this TextView. This is equivalent to the vertical distance between subsequent baselines in the TextView.
name android:lineHeight
setLineHeight
public static void setLineHeight(
@NonNull TextView textView,
int unit,
@FloatRange(from = 0) float lineHeight
)
Sets an explicit line height to a given unit and value for the TextView. This is equivalent to the vertical distance between subsequent baselines in the TextView. See TypedValue for the possible dimension units.
ref android.R.styleable#TextView_lineHeight
| Parameters | |
|---|---|
@NonNull TextView textView |
the TextView to modify |
int unit |
The desired dimension unit. SP units are strongly recommended so that line height stays proportional to the text size when fonts are scaled up for accessibility. |
@FloatRange(from = 0) float lineHeight |
The desired line height in the given units. |
| See also | |
|---|---|
setLineSpacing |
|
getLineSpacingExtra |
setPrecomputedText
public static void setPrecomputedText(
@NonNull TextView textView,
@NonNull PrecomputedTextCompat precomputed
)
Sets the PrecomputedTextCompat to the TextView If the given PrecomputeTextCompat is not compatible with textView, throws an IllegalArgumentException.
| Parameters | |
|---|---|
@NonNull TextView textView |
the TextView |
@NonNull PrecomputedTextCompat precomputed |
the precomputed text |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if precomputed text is not compatible with textView. |
setTextAppearance
public static void setTextAppearance(@NonNull TextView textView, @StyleRes int resId)
Sets the text appearance from the specified style resource.
Use a framework-defined TextAppearance style like @android:style/TextAppearance.Material.Body1.
setTextMetricsParams
public static void setTextMetricsParams(
@NonNull TextView textView,
@NonNull PrecomputedTextCompat.Params params
)
Apply the text layout parameter. Update the TextView parameters to be compatible with PrecomputedTextCompat.Params.
| See also | |
|---|---|
PrecomputedTextCompat |