TextViewCompat

class TextViewCompat
kotlin.Any
   ↳ androidx.core.widget.TextViewCompat

Helper for accessing features in TextView.

Summary

Constants

static Int

The TextView does not auto-size text (default).

static Int

The TextView scales text size both horizontally and vertically to fit within the container.

Public methods

static Int
getAutoSizeMaxTextSize(@NonNull textView: TextView)

static Int
getAutoSizeMinTextSize(@NonNull textView: TextView)

static Int

static IntArray

static Int
getAutoSizeTextType(@NonNull textView: TextView)

Returns the type of auto-size set for this widget.

static ColorStateList?

Return the tint applied to any compound drawables.

static Mode?

Return the tint mode applied to any compound drawables.

static Array<Drawable!>

Returns drawables for the start, top, end, and bottom borders from the given text view.

static Int

Returns the distance between the first text baseline and the top of this TextView.

static Int

Returns the distance between the last text baseline and the bottom of this TextView.

static Int
getMaxLines(@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.

static Int
getMinLines(@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.

static PrecomputedTextCompat.Params
getTextMetricsParams(@NonNull textView: TextView)

Gets the parameters for text layout precomputation, for use with PrecomputedTextCompat.

static Unit
setAutoSizeTextTypeUniformWithConfiguration(@NonNull textView: TextView, autoSizeMinTextSize: Int, autoSizeMaxTextSize: Int, autoSizeStepGranularity: Int, unit: Int)

Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds.

static Unit
setAutoSizeTextTypeUniformWithPresetSizes(@NonNull textView: TextView, @NonNull presetSizes: IntArray, unit: Int)

Specify whether this widget should automatically scale the text to try to perfectly fit within the layout bounds.

static Unit
setAutoSizeTextTypeWithDefaults(@NonNull textView: TextView, autoSizeTextType: Int)

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 Unit
setCompoundDrawableTintList(@NonNull textView: TextView, @Nullable tint: ColorStateList?)

Applies a tint to any compound drawables.

static Unit
setCompoundDrawableTintMode(@NonNull textView: TextView, @Nullable tintMode: Mode?)

Applies a tint mode to any compound drawables.

static Unit
setCompoundDrawablesRelative(@NonNull textView: TextView, @Nullable start: Drawable?, @Nullable top: Drawable?, @Nullable end: Drawable?, @Nullable bottom: Drawable?)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text.

static Unit
setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull textView: TextView, @Nullable start: Drawable?, @Nullable top: Drawable?, @Nullable end: Drawable?, @Nullable bottom: Drawable?)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text.

static Unit
setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull textView: TextView, @DrawableRes start: Int, @DrawableRes top: Int, @DrawableRes end: Int, @DrawableRes bottom: Int)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text.

static Unit
setCustomSelectionActionModeCallback(@NonNull textView: TextView, @NonNull callback: Callback)

Sets a selection action mode callback on a TextView.

static Unit
setFirstBaselineToTopHeight(@NonNull textView: TextView, @Px firstBaselineToTopHeight: Int)

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.

static Unit
setLastBaselineToBottomHeight(@NonNull textView: TextView, @Px lastBaselineToBottomHeight: Int)

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.

static Unit
setLineHeight(@NonNull textView: TextView, @Px lineHeight: Int)

Sets an explicit line height for this TextView.

static Unit
setPrecomputedText(@NonNull textView: TextView, @NonNull precomputed: PrecomputedTextCompat)

Sets the PrecomputedTextCompat to the TextView If the given PrecomputeTextCompat is not compatible with textView, throws an IllegalArgumentException.

static Unit
setTextAppearance(@NonNull textView: TextView, @StyleRes resId: Int)

Sets the text appearance from the specified style resource.

static Unit
setTextMetricsParams(@NonNull textView: TextView, @NonNull params: PrecomputedTextCompat.Params)

Apply the text layout parameter.

Constants

AUTO_SIZE_TEXT_TYPE_NONE

static val AUTO_SIZE_TEXT_TYPE_NONE: Int

The TextView does not auto-size text (default).

Value: TextView.AUTO_SIZE_TEXT_TYPE_NONE

AUTO_SIZE_TEXT_TYPE_UNIFORM

static val AUTO_SIZE_TEXT_TYPE_UNIFORM: Int

The TextView scales text size both horizontally and vertically to fit within the container.

Value: TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM

Public methods

getAutoSizeMaxTextSize

static fun getAutoSizeMaxTextSize(@NonNull textView: TextView): Int
Return
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 -1.

getAutoSizeMinTextSize

static fun getAutoSizeMinTextSize(@NonNull textView: TextView): Int
Return
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 -1.

getAutoSizeStepGranularity

static fun getAutoSizeStepGranularity(@NonNull textView: TextView): Int
Return
Int: the current auto-size step granularity in pixels.

getAutoSizeTextAvailableSizes

@NonNull static fun getAutoSizeTextAvailableSizes(@NonNull textView: TextView): IntArray
Return
IntArray: the current auto-size int sizes array (in pixels).

getAutoSizeTextType

static fun getAutoSizeTextType(@NonNull textView: TextView): Int

Returns the type of auto-size set for this widget.

Return
Int: an int corresponding to one of the auto-size types: TextViewCompat#AUTO_SIZE_TEXT_TYPE_NONE or TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM

getCompoundDrawableTintList

@Nullable static fun getCompoundDrawableTintList(@NonNull textView: TextView): ColorStateList?

Return the tint applied to any compound drawables.

Only returns meaningful info when running on API v23 or newer, or if textView implements the TintableCompoundDrawablesView interface.

getCompoundDrawableTintMode

@Nullable static fun getCompoundDrawableTintMode(@NonNull textView: TextView): Mode?

Return the tint mode applied to any compound drawables.

Only returns meaningful info when running on API v23 or newer, or if textView implements the TintableCompoundDrawablesView interface.

getCompoundDrawablesRelative

@NonNull static fun getCompoundDrawablesRelative(@NonNull textView: TextView): Array<Drawable!>

Returns drawables for the start, top, end, and bottom borders from the given text view.

getFirstBaselineToTopHeight

static fun getFirstBaselineToTopHeight(@NonNull textView: TextView): Int

Returns the distance between the first text baseline and the top of this TextView.

getLastBaselineToBottomHeight

static fun getLastBaselineToBottomHeight(@NonNull textView: TextView): Int

Returns the distance between the last text baseline and the bottom of this TextView.

getMaxLines

static fun getMaxLines(@NonNull textView: TextView): Int

Returns the maximum number of lines displayed in the given TextView, or -1 if the maximum height was set in pixels instead.

getMinLines

static fun getMinLines(@NonNull textView: TextView): Int

Returns the minimum number of lines displayed in the given TextView, or -1 if the minimum height was set in pixels instead.

getTextMetricsParams

@NonNull static fun getTextMetricsParams(@NonNull textView: TextView): PrecomputedTextCompat.Params

Gets the parameters for text layout precomputation, for use with PrecomputedTextCompat.

Return
PrecomputedTextCompat.Params: a current PrecomputedTextCompat.Params

setAutoSizeTextTypeUniformWithConfiguration

static fun setAutoSizeTextTypeUniformWithConfiguration(@NonNull textView: TextView, autoSizeMinTextSize: Int, autoSizeMaxTextSize: Int, autoSizeStepGranularity: Int, unit: 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 TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM.

Parameters
autoSizeMinTextSize TextView: the minimum text size available for auto-size
autoSizeMaxTextSize TextView: the maximum text size available for auto-size
autoSizeStepGranularity TextView: 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
unit TextView: the desired dimension unit for all sizes above. See TypedValue for the possible dimension units
Exceptions
IllegalArgumentException if any of the configuration params are invalid.

setAutoSizeTextTypeUniformWithPresetSizes

static fun setAutoSizeTextTypeUniformWithPresetSizes(@NonNull textView: TextView, @NonNull presetSizes: IntArray, unit: 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 TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM.

Parameters
presetSizes TextView: an int array of sizes in pixels
unit TextView: the desired dimension unit for the preset sizes above. See TypedValue for the possible dimension units
Exceptions
IllegalArgumentException if all of the presetSizes are invalid. _

setAutoSizeTextTypeWithDefaults

static fun setAutoSizeTextTypeWithDefaults(@NonNull textView: TextView, autoSizeTextType: Int): Unit

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.

Parameters
autoSizeTextType TextView: the type of auto-size. Must be one of TextViewCompat#AUTO_SIZE_TEXT_TYPE_NONE or TextViewCompat#AUTO_SIZE_TEXT_TYPE_UNIFORM

setCompoundDrawableTintList

static fun setCompoundDrawableTintList(@NonNull textView: TextView, @Nullable tint: ColorStateList?): Unit

Applies a tint to any compound drawables.

This will always take effect when running on API v23 or newer. When running on platforms previous to API v23, it will only take effect if textView implements the TintableCompoundDrawablesView interface.

setCompoundDrawableTintMode

static fun setCompoundDrawableTintMode(@NonNull textView: TextView, @Nullable tintMode: Mode?): Unit

Applies a tint mode to any compound drawables.

This will always take effect when running on API v23 or newer. When running on platforms previous to API v23, it will only take effect if textView implements the TintableCompoundDrawablesView interface.

setCompoundDrawablesRelative

static fun setCompoundDrawablesRelative(@NonNull textView: TextView, @Nullable start: Drawable?, @Nullable top: Drawable?, @Nullable end: Drawable?, @Nullable bottom: Drawable?): Unit

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 Drawable#setBounds called.

Calling this method will overwrite any Drawables previously set using TextView#setCompoundDrawables or related methods.
Parameters
textView TextView: The TextView against which to invoke the method.

setCompoundDrawablesRelativeWithIntrinsicBounds

static fun setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull textView: TextView, @Nullable start: Drawable?, @Nullable top: Drawable?, @Nullable end: Drawable?, @Nullable bottom: Drawable?): Unit

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.

Calling this method will overwrite any Drawables previously set using TextView#setCompoundDrawables or related methods.
Parameters
textView TextView: The TextView against which to invoke the method.

setCompoundDrawablesRelativeWithIntrinsicBounds

static fun setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull textView: TextView, @DrawableRes start: Int, @DrawableRes top: Int, @DrawableRes end: Int, @DrawableRes bottom: Int): Unit

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 using TextView#setCompoundDrawables or related methods.
Parameters
textView TextView: The TextView against which to invoke the method.
start TextView: Resource identifier of the start Drawable.
top TextView: Resource identifier of the top Drawable.
end TextView: Resource identifier of the end Drawable.
bottom TextView: Resource identifier of the bottom Drawable.

setCustomSelectionActionModeCallback

static fun setCustomSelectionActionModeCallback(@NonNull textView: TextView, @NonNull callback: Callback): Unit

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 < i. Intuitively, removing an element from the menu should behave as removing an element from a list. Note that this method does not exist in the Menu interface. However, it is required, and going to be called by reflection, in order to display the correct process text items in the menu.

Parameters
textView TextView: The TextView to set the action selection mode callback on.
callback TextView: The action selection mode callback to set on textView.

setFirstBaselineToTopHeight

static fun setFirstBaselineToTopHeight(@NonNull textView: TextView, @Px firstBaselineToTopHeight: Int): Unit

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.

Parameters
firstBaselineToTopHeight TextView: distance between first baseline to top of the container in pixels

setLastBaselineToBottomHeight

static fun setLastBaselineToBottomHeight(@NonNull textView: TextView, @Px lastBaselineToBottomHeight: Int): Unit

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.

Parameters
lastBaselineToBottomHeight TextView: distance between last baseline to bottom of the container in pixels

setLineHeight

static fun setLineHeight(@NonNull textView: TextView, @Px lineHeight: Int): Unit

Sets an explicit line height for this TextView. This is equivalent to the vertical distance between subsequent baselines in the TextView.

Parameters
lineHeight TextView: the line height in pixels

setPrecomputedText

static fun setPrecomputedText(@NonNull textView: TextView, @NonNull precomputed: PrecomputedTextCompat): Unit

Sets the PrecomputedTextCompat to the TextView If the given PrecomputeTextCompat is not compatible with textView, throws an IllegalArgumentException.

Parameters
textView TextView: the TextView
precomputed TextView: the precomputed text
Exceptions
IllegalArgumentException if precomputed text is not compatible with textView.

setTextAppearance

static fun setTextAppearance(@NonNull textView: TextView, @StyleRes resId: Int): Unit

Sets the text appearance from the specified style resource.

Use a framework-defined TextAppearance style like @android:style/TextAppearance.Material.Body1.

Parameters
textView TextView: The TextView against which to invoke the method.
resId TextView: The resource identifier of the style to apply.

setTextMetricsParams

static fun setTextMetricsParams(@NonNull textView: TextView, @NonNull params: PrecomputedTextCompat.Params): Unit

Apply the text layout parameter. Update the TextView parameters to be compatible with PrecomputedTextCompat.Params.