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
ColorStateList
|
getCompoundDrawableTintList(TextView textView)
Return the tint applied to any compound drawables. |
static
PorterDuff.Mode
|
getCompoundDrawableTintMode(TextView textView)
Return the tint mode applied to any compound drawables. |
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
PrecomputedTextCompat.Params
|
getTextMetricsParams(TextView textView)
Gets the parameters for text layout precomputation, for use with
|
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
|
setCompoundDrawableTintList(TextView textView, ColorStateList tint)
Applies a tint to any compound drawables. |
static
void
|
setCompoundDrawableTintMode(TextView textView, PorterDuff.Mode tintMode)
Applies a tint mode to any compound drawables. |
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, 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
|
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
|
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 |
static
void
|
setLastBaselineToBottomHeight(TextView textView, int lastBaselineToBottomHeight)
Updates the bottom padding of the TextView so that |
static
void
|
setLineHeight(TextView textView, int lineHeight)
Sets an explicit line height for this TextView. |
static
void
|
setPrecomputedText(TextView textView, PrecomputedTextCompat precomputed)
Sets the PrecomputedTextCompat to the TextView If the given PrecomputeTextCompat is not compatible with textView, throws an IllegalArgumentException. |
static
void
|
setTextAppearance(TextView textView, int resId)
Sets the text appearance from the specified style resource. |
static
void
|
setTextMetricsParams(TextView textView, PrecomputedTextCompat.Params params)
Apply the text layout parameter. |
Inherited methods | |
---|---|
Constants
AUTO_SIZE_TEXT_TYPE_NONE
public static final int AUTO_SIZE_TEXT_TYPE_NONE
The TextView does not auto-size text (default).
Constant Value: 0 (0x00000000)
AUTO_SIZE_TEXT_TYPE_UNIFORM
public static final 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
public static 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
public static 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
public static int getAutoSizeStepGranularity (TextView textView)
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int |
the current auto-size step granularity in pixels. |
getAutoSizeTextAvailableSizes
public static int[] getAutoSizeTextAvailableSizes (TextView textView)
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int[] |
the current auto-size int sizes array (in pixels). |
getAutoSizeTextType
public static 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 |
getCompoundDrawableTintList
public static ColorStateList getCompoundDrawableTintList (TextView textView)
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.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
ColorStateList |
getCompoundDrawableTintMode
public static PorterDuff.Mode getCompoundDrawableTintMode (TextView textView)
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.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
PorterDuff.Mode |
getCompoundDrawablesRelative
public static 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
public static int getFirstBaselineToTopHeight (TextView textView)
Returns the distance between the first text baseline and the top of this TextView.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int |
getLastBaselineToBottomHeight
public static int getLastBaselineToBottomHeight (TextView textView)
Returns the distance between the last text baseline and the bottom of this TextView.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int |
getMaxLines
public 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.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int |
getMinLines
public 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.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
int |
getTextMetricsParams
public static PrecomputedTextCompat.Params getTextMetricsParams (TextView textView)
Gets the parameters for text layout precomputation, for use with
PrecomputedTextCompat
.
Parameters | |
---|---|
textView |
TextView |
Returns | |
---|---|
PrecomputedTextCompat.Params |
a current PrecomputedTextCompat.Params |
See also:
setAutoSizeTextTypeUniformWithConfiguration
public 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. 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
public 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. 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
public 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.
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 |
setCompoundDrawableTintList
public static void setCompoundDrawableTintList (TextView textView, ColorStateList tint)
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.
Parameters | |
---|---|
textView |
TextView |
tint |
ColorStateList |
setCompoundDrawableTintMode
public static void setCompoundDrawableTintMode (TextView textView, PorterDuff.Mode tintMode)
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.
Parameters | |
---|---|
textView |
TextView |
tintMode |
PorterDuff.Mode |
setCompoundDrawablesRelative
public 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. Use null
if you do not want a Drawable
there. The Drawables must already have had Drawable.setBounds(Rect)
called.
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
public 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. 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 usingTextView.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. |
setCompoundDrawablesRelativeWithIntrinsicBounds
public 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. Use null
if you do not want a Drawable
there. The Drawables' bounds will be set to their intrinsic bounds.
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 |
setCustomSelectionActionModeCallback
public static 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/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 |
ActionMode.Callback : The action selection mode callback to set on textView.
|
setFirstBaselineToTopHeight
public 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.
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
public 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.
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
public static 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 |
setPrecomputedText
public static void setPrecomputedText (TextView textView, PrecomputedTextCompat precomputed)
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 | |
---|---|
IllegalArgumentException |
if precomputed text is not compatible with textView. |
setTextAppearance
public static 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.
|
setTextMetricsParams
public static void setTextMetricsParams (TextView textView, PrecomputedTextCompat.Params params)
Apply the text layout parameter.
Update the TextView parameters to be compatible with PrecomputedTextCompat.Params
.
Parameters | |
---|---|
textView |
TextView |
params |
PrecomputedTextCompat.Params |
See also: