TextViewCompat

Added in 1.1.0

class TextViewCompat


Helper for accessing features in TextView.

Summary

Constants

const Int

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

const Int

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

Public functions

java-static Int

name android:autoSizeMaxTextSize

java-static Int

name android:autoSizeMinTextSize

java-static Int

name android:autoSizeStepGranularity

java-static IntArray<Int>

name android:autoSizePresetSizes

java-static Int

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

java-static ColorStateList?

Return the tint applied to any compound drawables.

java-static PorterDuff.Mode?

Return the tint mode applied to any compound drawables.

java-static Array<Drawable!>
@ReplaceWith(expression = "textView.getCompoundDrawablesRelative()")
getCompoundDrawablesRelative(textView: TextView)

This function is deprecated.

Call getCompoundDrawablesRelative directly.

java-static Int

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

java-static Int

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

java-static Int
@ReplaceWith(expression = "textView.getMaxLines()")
getMaxLines(textView: TextView)

This function is deprecated.

Call getMaxLines directly.

java-static Int
@ReplaceWith(expression = "textView.getMinLines()")
getMinLines(textView: TextView)

This function is deprecated.

Call getMinLines directly.

java-static PrecomputedTextCompat.Params

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

java-static Unit
setAutoSizeTextTypeUniformWithConfiguration(
    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.

java-static Unit
setAutoSizeTextTypeUniformWithPresetSizes(
    textView: TextView,
    presetSizes: IntArray,
    unit: Int
)

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

java-static Unit
setAutoSizeTextTypeWithDefaults(
    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.

java-static Unit

Applies a tint to any compound drawables.

java-static Unit
setCompoundDrawableTintMode(
    textView: TextView,
    tintMode: PorterDuff.Mode?
)

Applies a tint mode to any compound drawables.

java-static Unit
@ReplaceWith(expression = "textView.setCompoundDrawablesRelative(start, top, end, bottom)")
setCompoundDrawablesRelative(
    textView: TextView,
    start: Drawable?,
    top: Drawable?,
    end: Drawable?,
    bottom: Drawable?
)

This function is deprecated.

Call setCompoundDrawablesRelative directly.

java-static Unit
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
setCompoundDrawablesRelativeWithIntrinsicBounds(
    textView: TextView,
    start: Drawable?,
    top: Drawable?,
    end: Drawable?,
    bottom: Drawable?
)

This function is deprecated.

Call setCompoundDrawablesRelativeWithIntrinsicBounds directly.

java-static Unit
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
setCompoundDrawablesRelativeWithIntrinsicBounds(
    textView: TextView,
    start: @DrawableRes Int,
    top: @DrawableRes Int,
    end: @DrawableRes Int,
    bottom: @DrawableRes Int
)

This function is deprecated.

Call setCompoundDrawablesRelativeWithIntrinsicBounds directly.

java-static Unit
@ReplaceWith(expression = "textView.setCustomSelectionActionModeCallback(callback)")
setCustomSelectionActionModeCallback(
    textView: TextView,
    callback: ActionMode.Callback
)

This function is deprecated.

Call setCustomSelectionActionModeCallback directly.

java-static Unit
setFirstBaselineToTopHeight(
    textView: TextView,
    firstBaselineToTopHeight: @Px @IntRange(from = 0) 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.

java-static Unit
setLastBaselineToBottomHeight(
    textView: TextView,
    lastBaselineToBottomHeight: @Px @IntRange(from = 0) 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.

java-static Unit
setLineHeight(textView: TextView, lineHeight: @Px @IntRange(from = 0) Int)

Sets an explicit line height for this TextView.

java-static Unit
setLineHeight(
    textView: TextView,
    unit: Int,
    lineHeight: @FloatRange(from = 0) Float
)

Sets an explicit line height to a given unit and value for the TextView.

java-static Unit
setPrecomputedText(
    textView: TextView,
    precomputed: PrecomputedTextCompat
)

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

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

Sets the text appearance from the specified style resource.

java-static Unit
setTextMetricsParams(
    textView: TextView,
    params: PrecomputedTextCompat.Params
)

Apply the text layout parameter.

Constants

AUTO_SIZE_TEXT_TYPE_NONE

Added in 1.1.0
const val AUTO_SIZE_TEXT_TYPE_NONE = 0: Int

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

AUTO_SIZE_TEXT_TYPE_UNIFORM

Added in 1.1.0
const val AUTO_SIZE_TEXT_TYPE_UNIFORM = 1: Int

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

Public functions

getAutoSizeMaxTextSize

Added in 1.1.0
java-static fun getAutoSizeMaxTextSize(textView: TextView): Int

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 -1.

getAutoSizeMinTextSize

Added in 1.1.0
java-static fun getAutoSizeMinTextSize(textView: TextView): Int

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 -1.

getAutoSizeStepGranularity

Added in 1.1.0
java-static fun getAutoSizeStepGranularity(textView: TextView): Int

name android:autoSizeStepGranularity

Returns
Int

the current auto-size step granularity in pixels.

getAutoSizeTextAvailableSizes

Added in 1.1.0
java-static fun getAutoSizeTextAvailableSizes(textView: TextView): IntArray<Int>

name android:autoSizePresetSizes

Returns
IntArray<Int>

the current auto-size int sizes array (in pixels).

getAutoSizeTextType

Added in 1.1.0
java-static fun getAutoSizeTextType(textView: TextView): Int

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

name android:autoSizeTextType

Returns
Int

an int corresponding to one of the auto-size types: AUTO_SIZE_TEXT_TYPE_NONE or AUTO_SIZE_TEXT_TYPE_UNIFORM

getCompoundDrawableTintList

Added in 1.1.0
java-static fun getCompoundDrawableTintList(textView: TextView): ColorStateList?

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

Added in 1.1.0
java-static fun getCompoundDrawableTintMode(textView: TextView): PorterDuff.Mode?

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.

getCompoundDrawablesRelative

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.getCompoundDrawablesRelative()")
java-static fun getCompoundDrawablesRelative(textView: TextView): Array<Drawable!>

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

getFirstBaselineToTopHeight

Added in 1.1.0
java-static fun getFirstBaselineToTopHeight(textView: TextView): Int

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

name android:firstBaselineToTopHeight

getLastBaselineToBottomHeight

Added in 1.1.0
java-static fun getLastBaselineToBottomHeight(textView: TextView): Int

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

name android:lastBaselineToBottomHeight

getMaxLines

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.getMaxLines()")
java-static fun getMaxLines(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

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.getMinLines()")
java-static fun getMinLines(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

Added in 1.1.0
java-static fun getTextMetricsParams(textView: TextView): PrecomputedTextCompat.Params

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

setAutoSizeTextTypeUniformWithConfiguration

Added in 1.1.0
java-static fun setAutoSizeTextTypeUniformWithConfiguration(
    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 AUTO_SIZE_TEXT_TYPE_UNIFORM.

name android:autoSizeTextType

name android:autoSizeTextType

name android:autoSizeMinTextSize

name android:autoSizeMaxTextSize

name android:autoSizeStepGranularity

Parameters
textView: TextView

TextView for which to set the mode.

autoSizeMinTextSize: Int

the minimum text size available for auto-size

autoSizeMaxTextSize: Int

the maximum text size available for auto-size

autoSizeStepGranularity: Int

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: Int

the desired dimension unit for all sizes above. See TypedValue for the possible dimension units

Throws
java.lang.IllegalArgumentException

if any of the configuration params are invalid.

setAutoSizeTextTypeUniformWithPresetSizes

Added in 1.1.0
java-static fun setAutoSizeTextTypeUniformWithPresetSizes(
    textView: TextView,
    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 AUTO_SIZE_TEXT_TYPE_UNIFORM.

name android:autoSizeTextType

name android:autoSizePresetSizes

Parameters
textView: TextView

TextView for which to set the mode.

presetSizes: IntArray

an int array of sizes in pixels

unit: Int

the desired dimension unit for the preset sizes above. See TypedValue for the possible dimension units

Throws
java.lang.IllegalArgumentException

if all of the presetSizes are invalid. _

setAutoSizeTextTypeWithDefaults

Added in 1.1.0
java-static fun setAutoSizeTextTypeWithDefaults(
    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.

name android:autoSizeTextType

Parameters
textView: TextView

TextView for which to set the mode.

autoSizeTextType: Int

the type of auto-size. Must be one of AUTO_SIZE_TEXT_TYPE_NONE or AUTO_SIZE_TEXT_TYPE_UNIFORM

setCompoundDrawableTintList

Added in 1.1.0
java-static fun setCompoundDrawableTintList(textView: TextView, tint: ColorStateList?): Unit

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

Added in 1.1.0
java-static fun setCompoundDrawableTintMode(
    textView: TextView,
    tintMode: PorterDuff.Mode?
): Unit

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.

setCompoundDrawablesRelative

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.setCompoundDrawablesRelative(start, top, end, bottom)")
java-static fun setCompoundDrawablesRelative(
    textView: TextView,
    start: Drawable?,
    top: Drawable?,
    end: Drawable?,
    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 setBounds called.

Calling this method will overwrite any Drawables previously set using setCompoundDrawables or related methods.

name android:drawableStart

name android:drawableTop

name android:drawableEnd

name android:drawableBottom

Parameters
textView: TextView

The TextView against which to invoke the method.

start: Drawable?

position in pixels of the start bound

top: Drawable?

position in pixels of the top bound

end: Drawable?

position in pixels of the end bound

bottom: Drawable?

position in pixels of the bottom bound

setCompoundDrawablesRelativeWithIntrinsicBounds

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
java-static fun setCompoundDrawablesRelativeWithIntrinsicBounds(
    textView: TextView,
    start: Drawable?,
    top: Drawable?,
    end: Drawable?,
    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 setCompoundDrawables or related methods.

name android:drawableStart

name android:drawableTop

name android:drawableEnd

name android:drawableBottom

Parameters
textView: TextView

The TextView against which to invoke the method.

start: Drawable?

drawable to use at start

top: Drawable?

drawable to use at top

end: Drawable?

drawable to use at end

bottom: Drawable?

drawable to use at bottom

setCompoundDrawablesRelativeWithIntrinsicBounds

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)")
java-static fun setCompoundDrawablesRelativeWithIntrinsicBounds(
    textView: TextView,
    start: @DrawableRes Int,
    top: @DrawableRes Int,
    end: @DrawableRes Int,
    bottom: @DrawableRes 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 setCompoundDrawables or related methods.

name android:drawableStart

name android:drawableTop

name android:drawableEnd

name android:drawableBottom

Parameters
textView: TextView

The TextView against which to invoke the method.

start: @DrawableRes Int

Resource identifier of the start Drawable.

top: @DrawableRes Int

Resource identifier of the top Drawable.

end: @DrawableRes Int

Resource identifier of the end Drawable.

bottom: @DrawableRes Int

Resource identifier of the bottom Drawable.

setCustomSelectionActionModeCallback

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "textView.setCustomSelectionActionModeCallback(callback)")
java-static fun setCustomSelectionActionModeCallback(
    textView: TextView,
    callback: ActionMode.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 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: ActionMode.Callback

The action selection mode callback to set on textView.

setFirstBaselineToTopHeight

Added in 1.1.0
java-static fun setFirstBaselineToTopHeight(
    textView: TextView,
    firstBaselineToTopHeight: @Px @IntRange(from = 0) 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.

name android:firstBaselineToTopHeight

Parameters
textView: TextView

TextView for which to set the padding.

firstBaselineToTopHeight: @Px @IntRange(from = 0) Int

distance between first baseline to top of the container in pixels

setLastBaselineToBottomHeight

Added in 1.1.0
java-static fun setLastBaselineToBottomHeight(
    textView: TextView,
    lastBaselineToBottomHeight: @Px @IntRange(from = 0) 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.

name android:lastBaselineToBottomHeight

Parameters
textView: TextView

TextView for which to set the padding.

lastBaselineToBottomHeight: @Px @IntRange(from = 0) Int

distance between last baseline to bottom of the container in pixels

setLineHeight

Added in 1.1.0
java-static fun setLineHeight(textView: TextView, lineHeight: @Px @IntRange(from = 0) Int): Unit

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

name android:lineHeight

Parameters
textView: TextView

the TextView to modify

lineHeight: @Px @IntRange(from = 0) Int

the line height in pixels

setLineHeight

Added in 1.12.0
java-static fun setLineHeight(
    textView: TextView,
    unit: Int,
    lineHeight: @FloatRange(from = 0) Float
): Unit

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 for the possible dimension units.

ref android.R.styleable#TextView_lineHeight

Parameters
textView: TextView

the TextView to modify

unit: Int

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.

lineHeight: @FloatRange(from = 0) Float

The desired line height in the given units.

setPrecomputedText

Added in 1.1.0
java-static fun setPrecomputedText(
    textView: TextView,
    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: PrecomputedTextCompat

the precomputed text

Throws
java.lang.IllegalArgumentException

if precomputed text is not compatible with textView.

setTextAppearance

Added in 1.1.0
java-static fun setTextAppearance(textView: TextView, resId: @StyleRes 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: @StyleRes Int

The resource identifier of the style to apply.

setTextMetricsParams

Added in 1.1.0
java-static fun setTextMetricsParams(
    textView: TextView,
    params: PrecomputedTextCompat.Params
): Unit

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