lightbulb_outline Please take our October 2018 developer survey. Start survey
added in version 22.1.0
belongs to Maven artifact com.android.support:support-compat:28.0.0-alpha1

TextViewCompat

public final class TextViewCompat
extends Object

java.lang.Object
   ↳ android.support.v4.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 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 setTextAppearance(TextView textView, int resId)

Sets the text appearance from the specified style resource.

Inherited methods

From class java.lang.Object

Constants

AUTO_SIZE_TEXT_TYPE_NONE

added in version 26.1.0
int AUTO_SIZE_TEXT_TYPE_NONE

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

Constant Value: 0 (0x00000000)

AUTO_SIZE_TEXT_TYPE_UNIFORM

added in version 26.1.0
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

added in version 26.1.0
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

added in version 26.1.0
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

added in version 26.1.0
int getAutoSizeStepGranularity (TextView textView)

Parameters
textView TextView

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

getAutoSizeTextAvailableSizes

added in version 26.1.0
int[] getAutoSizeTextAvailableSizes (TextView textView)

Parameters
textView TextView

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

getAutoSizeTextType

added in version 26.1.0
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

added in version 25.1.0
Drawable[] getCompoundDrawablesRelative (TextView textView)

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

Parameters
textView TextView

Returns
Drawable[]

getMaxLines

added in version 24.1.0
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

added in version 24.1.0
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

added in version 26.1.0
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

added in version 26.1.0
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

added in version 26.1.0
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

added in version 22.1.0
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 setBounds(Rect) called.

Calling this method will overwrite any Drawables previously set using 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

added in version 22.1.0
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 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

added in version 22.1.0
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 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

added in version 27.1.0
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.

setTextAppearance

added in version 24.1.0
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.