belongs to Maven artifact androidx.core:core:1.0.0-alpha1

TextViewCompat

public final class TextViewCompat
extends Object

java.lang.Object
   ↳ androidx.core.widget.TextViewCompat


Helper for accessing features in TextView.

Summary

Constants

int AUTO_SIZE_TEXT_TYPE_NONE

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

int AUTO_SIZE_TEXT_TYPE_UNIFORM

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

Public methods

static int getAutoSizeMaxTextSize(TextView textView)
static int getAutoSizeMinTextSize(TextView textView)
static int getAutoSizeStepGranularity(TextView textView)
static int[] getAutoSizeTextAvailableSizes(TextView textView)
static int getAutoSizeTextType(TextView textView)

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

static Drawable[] getCompoundDrawablesRelative(TextView textView)

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

static int getFirstBaselineToTopHeight(TextView textView)

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

static int getLastBaselineToBottomHeight(TextView textView)

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

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 void setAutoSizeTextTypeUniformWithConfiguration(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.

static void setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit)

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

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

static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top, Drawable end, Drawable bottom)

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

static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView, Drawable start, Drawable top, Drawable end, Drawable bottom)

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

static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView, int start, int top, int end, int bottom)

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

static void setCustomSelectionActionModeCallback(TextView textView, ActionMode.Callback callback)

Sets a selection action mode callback on a TextView.

static void setFirstBaselineToTopHeight(TextView textView, 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.

static void setLastBaselineToBottomHeight(TextView textView, 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.

static void setLineHeight(TextView textView, int lineHeight)

Sets an explicit line height for this TextView.

static void setTextAppearance(TextView textView, int resId)

Sets the text appearance from the specified style resource.

Inherited methods

Constants

AUTO_SIZE_TEXT_TYPE_NONE

int AUTO_SIZE_TEXT_TYPE_NONE

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

Constant Value: 0 (0x00000000)

AUTO_SIZE_TEXT_TYPE_UNIFORM

int AUTO_SIZE_TEXT_TYPE_UNIFORM

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

Constant Value: 1 (0x00000001)

Public methods

getAutoSizeMaxTextSize

int getAutoSizeMaxTextSize (TextView textView)

Parameters
textView TextView

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

int getAutoSizeMinTextSize (TextView textView)

Parameters
textView TextView

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

int getAutoSizeStepGranularity (TextView textView)

Parameters
textView TextView

Returns
int the current auto-size step granularity in pixels.

getAutoSizeTextAvailableSizes

int[] getAutoSizeTextAvailableSizes (TextView textView)

Parameters
textView TextView

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

getAutoSizeTextType

int getAutoSizeTextType (TextView textView)

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

Parameters
textView TextView

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

getCompoundDrawablesRelative

Drawable[] getCompoundDrawablesRelative (TextView textView)

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

Parameters
textView TextView

Returns
Drawable[]

getFirstBaselineToTopHeight

int getFirstBaselineToTopHeight (TextView textView)

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

Parameters
textView TextView

Returns
int

getLastBaselineToBottomHeight

int getLastBaselineToBottomHeight (TextView textView)

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

Parameters
textView TextView

Returns
int

getMaxLines

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.

Parameters
textView TextView

Returns
int

getMinLines

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.

Parameters
textView TextView

Returns
int

setAutoSizeTextTypeUniformWithConfiguration

void setAutoSizeTextTypeUniformWithConfiguration (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.

Parameters
textView TextView

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
IllegalArgumentException if any of the configuration params are invalid.

setAutoSizeTextTypeUniformWithPresetSizes

void setAutoSizeTextTypeUniformWithPresetSizes (TextView textView, 
                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.

Parameters
textView TextView

presetSizes int: 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
IllegalArgumentException if all of the presetSizes are invalid. _

setAutoSizeTextTypeWithDefaults

void setAutoSizeTextTypeWithDefaults (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.

Parameters
textView TextView

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

setCompoundDrawablesRelative

void setCompoundDrawablesRelative (TextView textView, 
                Drawable start, 
                Drawable top, 
                Drawable end, 
                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 Drawable.setBounds(Rect) called.

Calling this method will overwrite any Drawables previously set using TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) or related methods.

Parameters
textView TextView: The TextView against which to invoke the method.

start Drawable

top Drawable

end Drawable

bottom Drawable

setCompoundDrawablesRelativeWithIntrinsicBounds

void setCompoundDrawablesRelativeWithIntrinsicBounds (TextView textView, 
                Drawable start, 
                Drawable top, 
                Drawable end, 
                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.

Calling this method will overwrite any Drawables previously set using TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) or related methods.

Parameters
textView TextView: The TextView against which to invoke the method.

start Drawable

top Drawable

end Drawable

bottom Drawable

setCompoundDrawablesRelativeWithIntrinsicBounds

void setCompoundDrawablesRelativeWithIntrinsicBounds (TextView textView, 
                int start, 
                int top, 
                int end, 
                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 using TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) or related methods.

Parameters
textView TextView: The TextView against which to invoke the method.

start int: Resource identifier of the start Drawable.

top int: Resource identifier of the top Drawable.

end int: Resource identifier of the end Drawable.

bottom int: Resource identifier of the bottom Drawable.

setCustomSelectionActionModeCallback

void setCustomSelectionActionModeCallback (TextView textView, 
                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. 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 ActionMode.Callback: The action selection mode callback to set on textView.

setFirstBaselineToTopHeight

void setFirstBaselineToTopHeight (TextView textView, 
                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.

Parameters
textView TextView

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

setLastBaselineToBottomHeight

void setLastBaselineToBottomHeight (TextView textView, 
                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.

Parameters
textView TextView

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

setLineHeight

void setLineHeight (TextView textView, 
                int lineHeight)

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

Parameters
textView TextView

lineHeight int: the line height in pixels

setTextAppearance

void setTextAppearance (TextView textView, 
                int resId)

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 int: The resource identifier of the style to apply.