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 Unit
setCompoundDrawablesRelative(textView: TextView, start: Drawable?, top: Drawable?, end: Drawable?, bottom: Drawable?)

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

static Unit

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

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

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

static Int
getMaxLines(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(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 Unit
setTextAppearance(textView: TextView, resId: Int)

Sets the text appearance from the specified style resource.

static Array<Drawable!>

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

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.

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.

static Unit

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

static Int

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

static Int

static Int

static Int

static IntArray

static Unit

Sets a selection action mode callback on a TextView.

static Unit
setFirstBaselineToTopHeight(textView: TextView, 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(textView: TextView, 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 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 Unit
setLineHeight(textView: TextView, lineHeight: Int)

Sets an explicit line height for this TextView.

Constants

AUTO_SIZE_TEXT_TYPE_NONE

static val AUTO_SIZE_TEXT_TYPE_NONE: Int

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

Value: 0

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

Public methods

setCompoundDrawablesRelative

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 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(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 TextView#setCompoundDrawables or related methods.
Parameters
textView TextView: The TextView against which to invoke the method.

setCompoundDrawablesRelativeWithIntrinsicBounds

static fun setCompoundDrawablesRelativeWithIntrinsicBounds(textView: TextView, start: Int, top: Int, end: Int, 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.

getMaxLines

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

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.

setTextAppearance

static fun setTextAppearance(textView: TextView, 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.

getCompoundDrawablesRelative

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

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

setAutoSizeTextTypeWithDefaults

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.

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

setAutoSizeTextTypeUniformWithConfiguration

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 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(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 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. _

getAutoSizeTextType

static fun getAutoSizeTextType(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

getAutoSizeStepGranularity

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

getAutoSizeMinTextSize

static fun getAutoSizeMinTextSize(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.

getAutoSizeMaxTextSize

static fun getAutoSizeMaxTextSize(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.

getAutoSizeTextAvailableSizes

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

setCustomSelectionActionModeCallback

static fun setCustomSelectionActionModeCallback(textView: TextView, 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(textView: TextView, 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(textView: TextView, 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

getFirstBaselineToTopHeight

static fun getFirstBaselineToTopHeight(textView: TextView): Int

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

getLastBaselineToBottomHeight

static fun getLastBaselineToBottomHeight(textView: TextView): Int

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

setLineHeight

static fun setLineHeight(textView: TextView, 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