Displays text to the user and optionally allows them to edit it. A TextView
is a complete text editor, however the basic class is configured to not
allow editing; see
EditText for a subclass that configures the text
view for editing.
XML attributes
See TextView Attributes,
View Attributes
- Attr:
- ref android.R.styleable#TextView_text
- Attr:
- ref android.R.styleable#TextView_bufferType
- Attr:
- ref android.R.styleable#TextView_hint
- Attr:
- ref android.R.styleable#TextView_textColor
- Attr:
- ref android.R.styleable#TextView_textColorHighlight
- Attr:
- ref android.R.styleable#TextView_textColorHint
- Attr:
- ref android.R.styleable#TextView_textAppearance
- Attr:
- ref android.R.styleable#TextView_textColorLink
- Attr:
- ref android.R.styleable#TextView_textSize
- Attr:
- ref android.R.styleable#TextView_textScaleX
- Attr:
- ref android.R.styleable#TextView_typeface
- Attr:
- ref android.R.styleable#TextView_textStyle
- Attr:
- ref android.R.styleable#TextView_cursorVisible
- Attr:
- ref android.R.styleable#TextView_maxLines
- Attr:
- ref android.R.styleable#TextView_maxHeight
- Attr:
- ref android.R.styleable#TextView_lines
- Attr:
- ref android.R.styleable#TextView_height
- Attr:
- ref android.R.styleable#TextView_minLines
- Attr:
- ref android.R.styleable#TextView_minHeight
- Attr:
- ref android.R.styleable#TextView_maxEms
- Attr:
- ref android.R.styleable#TextView_maxWidth
- Attr:
- ref android.R.styleable#TextView_ems
- Attr:
- ref android.R.styleable#TextView_width
- Attr:
- ref android.R.styleable#TextView_minEms
- Attr:
- ref android.R.styleable#TextView_minWidth
- Attr:
- ref android.R.styleable#TextView_gravity
- Attr:
- ref android.R.styleable#TextView_scrollHorizontally
- Attr:
- ref android.R.styleable#TextView_password
- Attr:
- ref android.R.styleable#TextView_singleLine
- Attr:
- ref android.R.styleable#TextView_selectAllOnFocus
- Attr:
- ref android.R.styleable#TextView_includeFontPadding
- Attr:
- ref android.R.styleable#TextView_maxLength
- Attr:
- ref android.R.styleable#TextView_shadowColor
- Attr:
- ref android.R.styleable#TextView_shadowDx
- Attr:
- ref android.R.styleable#TextView_shadowDy
- Attr:
- ref android.R.styleable#TextView_shadowRadius
- Attr:
- ref android.R.styleable#TextView_autoLink
- Attr:
- ref android.R.styleable#TextView_linksClickable
- Attr:
- ref android.R.styleable#TextView_numeric
- Attr:
- ref android.R.styleable#TextView_digits
- Attr:
- ref android.R.styleable#TextView_phoneNumber
- Attr:
- ref android.R.styleable#TextView_inputMethod
- Attr:
- ref android.R.styleable#TextView_capitalize
- Attr:
- ref android.R.styleable#TextView_autoText
- Attr:
- ref android.R.styleable#TextView_editable
- Attr:
- ref android.R.styleable#TextView_freezesText
- Attr:
- ref android.R.styleable#TextView_ellipsize
- Attr:
- ref android.R.styleable#TextView_drawableTop
- Attr:
- ref android.R.styleable#TextView_drawableBottom
- Attr:
- ref android.R.styleable#TextView_drawableRight
- Attr:
- ref android.R.styleable#TextView_drawableLeft
- Attr:
- ref android.R.styleable#TextView_drawablePadding
- Attr:
- ref android.R.styleable#TextView_lineSpacingExtra
- Attr:
- ref android.R.styleable#TextView_lineSpacingMultiplier
- Attr:
- ref android.R.styleable#TextView_marqueeRepeatLimit
- Attr:
- ref android.R.styleable#TextView_inputType
- Attr:
- ref android.R.styleable#TextView_imeOptions
- Attr:
- ref android.R.styleable#TextView_privateImeOptions
- Attr:
- ref android.R.styleable#TextView_imeActionLabel
- Attr:
- ref android.R.styleable#TextView_imeActionId
- Attr:
- ref android.R.styleable#TextView_editorExtras
private static final int SANS = 1;
private static final int SERIF = 2;
private static final int SIGNED = 2;
private static final int DECIMAL = 4;
This flag is set if the TextView tries to display an error before it
is attached to the window (so its position is still unknown).
It causes the error to be shown later, when onAttachedToWindow()
is called.
Interface definition for a callback to be invoked when an action is
performed on the editor.
Called when an action is being performed.
- Parameters:
v The view that was clicked.actionId Identifier of the action. This will be either the
identifier you supplied, or EditorInfo.IME_NULL if being called due to the enter key
being pressed.event If triggered by an enter key, this is the event;
otherwise, this is null.- Returns:
- Return true if you have consumed the action, else false.
super(context, attrs, defStyle);
int textColorHighlight = 0;
if (appearance != null) { for (int i = 0; i < n; i++) { textColorHighlight = appearance.getColor(attr, textColorHighlight);
typefaceIndex = appearance.getInt(attr, -1);
styleIndex = appearance.getInt(attr, -1);
boolean autotext = false;
boolean selectallonfocus = false;
Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
boolean singleLine = false;
float dx = 0, dy = 0, r = 0;
boolean password = false;
for (int i = 0; i < n; i++) { numeric = a.getInt(attr, numeric);
autocap = a.getInt(attr, autocap);
buffertype = a.getInt(attr, buffertype);
selectallonfocus = a.getBoolean(attr, selectallonfocus);
ellipsize = a.getInt(attr, ellipsize);
maxlength = a.getInt(attr, -1);
shadowcolor = a.getInt(attr, 0);
textColorHighlight = a.getColor(attr, textColorHighlight);
typefaceIndex = a.getInt(attr, typefaceIndex);
styleIndex = a.getInt(attr, styleIndex);
Log.w(LOG_TAG, "Failure reading input extras", e);
Log.w(LOG_TAG, "Failure reading input extras", e);
if (inputMethod != null) { } else if (digits != null) { } else if (numeric != 0) { if ((numeric & SIGNED) != 0) { } else if (autotext || autocap != -1) { drawableLeft, drawableTop, drawableRight, drawableBottom);
if (mInput == null && ellipsize < 0) { if (textColorHighlight != 0) { boolean clickable = focusable;
boolean longClickable = focusable;
for (int i = 0; i < n; i++) { longClickable = a.getBoolean(attr, longClickable);
Sets the typeface and style in which the text should be displayed,
and turns on the fake bold and italic bits in the Paint if the
Typeface that you provided does not have all the bits in the
style that you specified.
- Attr:
- ref android.R.styleable#TextView_typeface
- Attr:
- ref android.R.styleable#TextView_textStyle
tf = Typeface.create(tf, style);
int typefaceStyle = tf != null ? tf.getStyle() : 0;
int need = style & ~typefaceStyle;
Subclasses override this to specify that they have a KeyListener
by default even if not specifically called for in the XML options.
Subclasses override this to specify a default movement method.
Return the text the TextView is displaying. If setText() was called with
an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast
the return value from this method to Spannable or Editable, respectively.
Note: The content of the return value should not be modified. If you want
a modifiable one, you should make your own copy first.
Returns the length, in characters, of the text managed by this TextView
Return the text the TextView is displaying as an Editable object. If
the text is not editable, null is returned.
- Returns:
- the height of one standard line in pixels. Note that markup
within the text can cause individual lines to be taller or shorter
than this height, and the layout may contain additional first-
or last-line padding.
- Returns:
- the Layout that is currently being used to display the text.
This can be null if the text or width has recently changes.
- Returns:
- the current key listener for this TextView.
This will frequently be null for non-EditText TextViews.
Sets the key listener to be used with this TextView. This can be null
to disallow user input. Note that this method has significant and
subtle interactions with soft keyboards and other input method:
see
KeyListener.getContentType()
for important details. Calling this method will replace the current
content type of the text view with the content type returned by the
key listener.
Be warned that if you want a TextView with a key listener or movement
method not to be focusable, or if you want a TextView without a
key listener or movement method to be focusable, you must call
android.view.View.setFocusable(boolean) again after calling this to get the focusability
back the way you want it.
- Attr:
- ref android.R.styleable#TextView_numeric
- Attr:
- ref android.R.styleable#TextView_digits
- Attr:
- ref android.R.styleable#TextView_phoneNumber
- Attr:
- ref android.R.styleable#TextView_inputMethod
- Attr:
- ref android.R.styleable#TextView_capitalize
- Attr:
- ref android.R.styleable#TextView_autoText
- Returns:
- the movement method being used for this TextView.
This will frequently be null for non-EditText TextViews.
Sets the movement method (arrow key handler) to be used for
this TextView. This can be null to disallow using the arrow keys
to move the cursor or scroll the view.
Be warned that if you want a TextView with a key listener or movement
method not to be focusable, or if you want a TextView without a
key listener or movement method to be focusable, you must call
android.view.View.setFocusable(boolean) again after calling this to get the focusability
back the way you want it.
- Returns:
- the current transformation method for this TextView.
This will frequently be null except for single-line and password
fields.
Sets the transformation that is applied to the text that this
TextView is displaying.
- Attr:
- ref android.R.styleable#TextView_password
- Attr:
- ref android.R.styleable#TextView_singleLine
Returns the top padding of the view, plus space for the top
Drawable if any.
if (dr == null || dr.mDrawableTop == null) { return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop;
Returns the bottom padding of the view, plus space for the bottom
Drawable if any.
if (dr == null || dr.mDrawableBottom == null) { Returns the left padding of the view, plus space for the left
Drawable if any.
if (dr == null || dr.mDrawableLeft == null) { return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft;
Returns the right padding of the view, plus space for the right
Drawable if any.
if (dr == null || dr.mDrawableRight == null) { return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight;
Returns the extended top padding of the view, including both the
top Drawable if any and any extra space to keep more than maxLines
of text from showing. It is only valid to call this after measuring.
if (layoutht >= viewht) { return top + viewht - layoutht;
return top + (viewht - layoutht) / 2;
Returns the extended bottom padding of the view, including both the
bottom Drawable if any and any extra space to keep more than maxLines
of text from showing. It is only valid to call this after measuring.
if (layoutht >= viewht) { return bottom + viewht - layoutht;
return bottom + (viewht - layoutht) / 2;
Returns the total left padding of the view, including the left
Drawable if any.
Returns the total right padding of the view, including the right
Drawable if any.
Returns the total top padding of the view, including the top
Drawable if any, the extra space to keep more than maxLines
from showing, and the vertical offset for gravity, if any.
Returns the total bottom padding of the view, including the bottom
Drawable if any, the extra space to keep more than maxLines
from showing, and the vertical offset for gravity, if any.
Sets the Drawables (if any) to appear to the left of, above,
to the right of, and below the text. Use null if you do not
want a Drawable there. The Drawables must already have had
android.graphics.drawable.Drawable.setBounds(android.graphics.Rect) called.
- Attr:
- ref android.R.styleable#TextView_drawableLeft
- Attr:
- ref android.R.styleable#TextView_drawableTop
- Attr:
- ref android.R.styleable#TextView_drawableRight
- Attr:
- ref android.R.styleable#TextView_drawableBottom
final boolean drawables = left != null || top != null
|| right != null || bottom != null;
if (dr.mDrawablePadding == 0) { if (dr.mDrawableLeft != null) dr.mDrawableLeft.setCallback(null);
if (dr.mDrawableTop != null) dr.mDrawableTop.setCallback(null);
if (dr.mDrawableRight != null) dr.mDrawableRight.setCallback(null);
dr.mDrawableRight = null;
if (dr.mDrawableBottom != null) dr.mDrawableBottom.setCallback(null);
dr.mDrawableBottom = null;
dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) { if (dr.mDrawableTop != top && dr.mDrawableTop != null) { if (dr.mDrawableRight != right && dr.mDrawableRight != null) { dr.mDrawableRight = right;
if (dr.mDrawableBottom != bottom && dr.mDrawableBottom != null) { dr.mDrawableBottom = bottom;
final Rect compoundRect = dr.mCompoundRect;
dr.mDrawableSizeLeft = compoundRect.width();
dr.mDrawableHeightLeft = compoundRect.height();
dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
dr.mDrawableSizeRight = compoundRect.width();
dr.mDrawableHeightRight = compoundRect.height();
dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
dr.mDrawableSizeTop = compoundRect.height();
dr.mDrawableWidthTop = compoundRect.width();
dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
dr.mDrawableSizeBottom = compoundRect.height();
dr.mDrawableWidthBottom = compoundRect.width();
dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
Sets the Drawables (if any) to appear to the left of, above,
to the right 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.
- Parameters:
left Resource identifier of the left Drawable.top Resource identifier of the top Drawable.right Resource identifier of the right Drawable.bottom Resource identifier of the bottom Drawable.- Attr:
- ref android.R.styleable#TextView_drawableLeft
- Attr:
- ref android.R.styleable#TextView_drawableTop
- Attr:
- ref android.R.styleable#TextView_drawableRight
- Attr:
- ref android.R.styleable#TextView_drawableBottom
Sets the Drawables (if any) to appear to the left of, above,
to the right 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.
- Attr:
- ref android.R.styleable#TextView_drawableLeft
- Attr:
- ref android.R.styleable#TextView_drawableTop
- Attr:
- ref android.R.styleable#TextView_drawableRight
- Attr:
- ref android.R.styleable#TextView_drawableBottom
Returns drawables for the left, top, right, and bottom borders.
dr.mDrawableLeft, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom
return new Drawable[] { null, null, null, null }; Sets the size of the padding between the compound drawables and
the text.
- Attr:
- ref android.R.styleable#TextView_drawablePadding
dr.mDrawablePadding = pad;
dr.mDrawablePadding = pad;
Returns the padding between the compound drawables and the text.
return dr != null ? dr.mDrawablePadding : 0;
public void setPadding(int left, int top, int right, int bottom) { Gets the autolink mask of the text. See
android.text.util.Linkify.ALL and peers for
possible values.
- Attr:
- ref android.R.styleable#TextView_autoLink
Sets the text color, size, style, hint color, and highlight color
from the specified TextAppearance resource.
int typefaceIndex, styleIndex;
- Returns:
- the size (in pixels) of the default text size in this TextView.
Set the default text size to the given value, interpreted as "scaled
pixel" units. This size is adjusted based on the current density and
user font size preference.
- Parameters:
size The scaled pixel size.- Attr:
- ref android.R.styleable#TextView_textSize
Set the default text size to a given unit and value. See
android.util.TypedValue for the possible dimension units.
- Parameters:
unit The desired dimension unit.size The desired size in the given units.- Attr:
- ref android.R.styleable#TextView_textSize
- Returns:
- the extent by which text is currently being stretched
horizontally. This will usually be 1.
Sets the extent by which text should be stretched horizontally.
- Attr:
- ref android.R.styleable#TextView_textScaleX
Sets the typeface and style in which the text should be displayed.
Note that not all Typeface families actually have bold and italic
variants, so you may need to use
setTypeface(android.graphics.Typeface,int) to get the appearance
that you actually want.
- Attr:
- ref android.R.styleable#TextView_typeface
- Attr:
- ref android.R.styleable#TextView_textStyle
- Returns:
- the current typeface and style in which the text is being
displayed.
Sets the text color for all the states (normal, selected,
focused) to be this color.
- Attr:
- ref android.R.styleable#TextView_textColor
Sets the text color.
- Attr:
- ref android.R.styleable#TextView_textColor
Return the set of text colors.
- Returns:
- Returns the set of text colors.
Return the current color selected for normal text.
- Returns:
- Returns the current text color.
Sets the color used to display the selection highlight.
- Attr:
- ref android.R.styleable#TextView_textColorHighlight
Gives the text a shadow of the specified radius and color, the specified
distance from its normal position.
- Attr:
- ref android.R.styleable#TextView_shadowColor
- Attr:
- ref android.R.styleable#TextView_shadowDx
- Attr:
- ref android.R.styleable#TextView_shadowDy
- Attr:
- ref android.R.styleable#TextView_shadowRadius
public void setShadowLayer(float radius, float dx, float dy, int color) { - Returns:
- the base paint used for the text. Please use this only to
consult the Paint's properties and not to change them.
Sets the autolink mask of the text. See
android.text.util.Linkify.ALL and peers for
possible values.
- Attr:
- ref android.R.styleable#TextView_autoLink
Sets the color of the hint text.
- Attr:
- ref android.R.styleable#TextView_textColorHint
Sets the color of the hint text.
- Attr:
- ref android.R.styleable#TextView_textColorHint
Return the color used to paint the hint text.
- Returns:
- Returns the list of hint text colors.
Return the current color selected to paint the hint text.
- Returns:
- Returns the current hint text color.
Sets the color of links in the text.
- Attr:
- ref android.R.styleable#TextView_textColorLink
Sets the color of links in the text.
- Attr:
- ref android.R.styleable#TextView_textColorLink
Returns the color used to paint links in the text.
- Returns:
- Returns the list of link text colors.
Sets the horizontal alignment of the text and the
vertical gravity that will be used when there is extra space
in the TextView beyond what is required for the text itself.
boolean newLayout = false;
if (mLayout != null && newLayout) { Returns the horizontal and vertical alignment of this TextView.
Sets flags on the Paint being used to display the text and
reflows the text if they are different from the old flags.
Sets whether the text should be allowed to be wider than the
View is. If false, it will be wrapped to the width of the View.
- Attr:
- ref android.R.styleable#TextView_scrollHorizontally
Makes the TextView at least this many lines tall
- Attr:
- ref android.R.styleable#TextView_minLines
Makes the TextView at least this many pixels tall
- Attr:
- ref android.R.styleable#TextView_minHeight
Makes the TextView at most this many lines tall
- Attr:
- ref android.R.styleable#TextView_maxLines
Makes the TextView at most this many pixels tall
- Attr:
- ref android.R.styleable#TextView_maxHeight
Makes the TextView exactly this many lines tall
- Attr:
- ref android.R.styleable#TextView_lines
Makes the TextView exactly this many pixels tall.
You could do the same thing by specifying this number in the
LayoutParams.
- Attr:
- ref android.R.styleable#TextView_height
Makes the TextView at least this many ems wide
- Attr:
- ref android.R.styleable#TextView_minEms
Makes the TextView at least this many pixels wide
- Attr:
- ref android.R.styleable#TextView_minWidth
Makes the TextView at most this many ems wide
- Attr:
- ref android.R.styleable#TextView_maxEms
Makes the TextView at most this many pixels wide
- Attr:
- ref android.R.styleable#TextView_maxWidth
Makes the TextView exactly this many ems wide
- Attr:
- ref android.R.styleable#TextView_ems
Makes the TextView exactly this many pixels wide.
You could do the same thing by specifying this number in the
LayoutParams.
- Attr:
- ref android.R.styleable#TextView_width
Sets line spacing for this TextView. Each line will have its height
multiplied by
mult and have
add added to it.
- Attr:
- ref android.R.styleable#TextView_lineSpacingExtra
- Attr:
- ref android.R.styleable#TextView_lineSpacingMultiplier
Convenience method: Append the specified text to the TextView's
display buffer, upgrading it to BufferType.EDITABLE if it was
not already editable.
Convenience method: Append the specified text slice to the TextView's
display buffer, upgrading it to BufferType.EDITABLE if it was
not already editable.
if (dr.mDrawableTop != null && dr.mDrawableTop.isStateful()) { if (dr.mDrawableBottom != null && dr.mDrawableBottom.isStateful()) { if (dr.mDrawableLeft != null && dr.mDrawableLeft.isStateful()) { if (dr.mDrawableRight != null && dr.mDrawableRight.isStateful()) { String str = "TextView.SavedState{" if (start >= 0 || end >= 0) { ss.frozenWithFocus = true;
if (ss.selStart >= 0 && ss.selEnd >= 0) { if (ss.selStart > len || ss.selEnd > len) { restored = "(restored) ";
Log.e(LOG_TAG, "Saved cursor position " + ss.selStart +
"/" + ss.selEnd + " out of range for " + restored +
if (ss.frozenWithFocus) { Control whether this text view saves its entire text contents when
freezing to an icicle, in addition to dynamic state such as cursor
position. By default this is false, not saving the text. Set to true
if the text in the text view is not being saved somewhere else in
persistent storage (such as in a content provider) so that if the
view is later thawed the user will not lose their data.
- Parameters:
freezesText Controls whether a frozen icicle should include the
entire text data: true to include it, false to not.- Attr:
- ref android.R.styleable#TextView_freezesText
Return whether this text view is including its entire text contents
in frozen icicles.
Sets the Factory used to create new Editables.
Sets the Factory used to create new Spannables.
Sets the string value of the TextView. TextView
does not accept
HTML-like formatting, which you can do with text strings in XML resource files.
To style your strings, attach android.text.style.* objects to a
SpannableString, or see the
Available Resource Types documentation for an example of setting
formatted text in the XML resource file.
- Attr:
- ref android.R.styleable#TextView_text
Sets the text that this TextView is to display (see
setText(java.lang.CharSequence)) and also sets whether it is stored
in a styleable/spannable buffer and whether it is editable.
- Attr:
- ref android.R.styleable#TextView_text
- Attr:
- ref android.R.styleable#TextView_bufferType
boolean notifyBefore, int oldlen) { for (int i = 0; i < n; i++) { boolean needEditableForNotification = false;
needEditableForNotification = true;
needEditableForNotification) { final int textLength = text.length();
final int count = watchers.length;
for (int i = 0; i < count; i++)
if (needEditableForNotification) { Sets the TextView to display the specified slice of the specified
char array. You must promise that you will not change the contents
of the array except for right before another call to setText(),
since the TextView has no way to know that the text
has changed and that it needs to invalidate and re-layout.
public final void setText(char[] text, int start, int len) { if (start < 0 || len < 0 || start + len > text.length) { void set(char[] chars, int start, int len) { public void getChars(int start, int end, char[] buf, int off) { float x, float y, Paint p) { if (start >= 0 || end >= 0) { Math.max(0, Math.min(start, len)),
Math.max(0, Math.min(end, len)));
public final void setText(int resid) { Sets the text to be displayed when the text of the TextView is empty.
Null means to use the normal empty text. The hint does not currently
participate in determining the size of the view.
- Attr:
- ref android.R.styleable#TextView_hint
Sets the text to be displayed when the text of the TextView is empty,
from a resource.
- Attr:
- ref android.R.styleable#TextView_hint
public final void setHint(int resid) { Returns the hint that is displayed when the text of the TextView
is empty.
- Attr:
- ref android.R.styleable#TextView_hint
boolean forceUpdate = false;
} else if (isVisiblePassword) { } else if (wasPassword || wasVisiblePassword) { It would be better to rely on the input type for everything. A password inputType should have
a password transformation. We should hence use isPasswordInputType instead of this method.
We should:
- Call setInputType in setKeyListener instead of changing the input type directly (which
would install the correct transformation).
- Refuse the installation of a non-password transformation in setTransformation if the input
type is password.
However, this is like this for legacy reasons and we cannot break existing apps. This method
is useful since it matches what the user can see (obfuscated text or not).
- Returns:
- true if the current transformation method is of the password type.
Directly change the content type integer of the text view, without
modifying any other state.
Get the type of the content.
Get the type of the IME editor.
Set a special listener to be called when an action is performed
on the text view. This will be called when the enter key is pressed,
or when an action supplied to the IME is selected by the user. Setting
this means that the normal hard key event will not insert a newline
into the text view, even if it is multi-line; holding down the ALT
modifier will, however, allow the user to insert a newline character.
Called when an attached input method calls
InputConnection.performEditorAction()
for this text view. The default implementation will call your action
listener supplied to
setOnEditorActionListener(android.widget.TextView.OnEditorActionListener), or perform
a standard operation for
EditorInfo.IME_ACTION_NEXT or
EditorInfo.IME_ACTION_DONE.
For backwards compatibility, if no IME options have been set and the
text view would not normally advance focus on enter, then
the NEXT and DONE actions received here will be turned into an enter
key down/up pair to go through the normal key handling.
if (ict.onEditorActionListener != null) { "that wasn't able to take focus!");
Set the private content type of the text, which is the
EditorInfo.privateImeOptions
field that will be filled in when creating an input connection.
Get the private type of the content.
Set the extra input data of the text, which is the
TextBoxAttribute.extras
Bundle that will be filled in when creating an input connection. The
given integer is the resource ID of an XML resource holding an
<input-extras> XML tree.
Retrieve the input extras currently associated with the text view, which
can be viewed as well as modified.
if (!create) return null;
if (!create) return null;
Returns the error message that was set to be displayed with
setError(java.lang.CharSequence), or
null if no error was set
or if it the error was cleared by the widget after user input.
Sets the right-hand compound drawable of the TextView to the "error"
icon and sets an error message that will be displayed in a popup when
the TextView has focus. The icon and error message will be reset to
null when any key events cause changes to the TextView's text. If the
error is
null, the error message and icon
will be cleared.
Sets the right-hand compound drawable of the TextView to the specified
icon and sets an error message that will be displayed in a popup when
the TextView has focus. The icon and error message will be reset to
null when any key events cause changes to the TextView's text. The
drawable must already have had
android.graphics.drawable.Drawable.setBounds(android.graphics.Rect) set on it.
If the
error is
null, the error message will
be cleared (and you should provide a
null icon as well).
icon, dr.mDrawableBottom);
(int) (50 * scale + 0.5f));
private boolean mAbove = false;
public void update(int x, int y, int w, int h, boolean force) { super.update(x, y, w, h, force);
Returns the Y offset to make the pointy top of the error point
at the middle of the error icon.
- (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f);
Returns the Y offset to make the pointy top of the error point
at the bottom of the error icon.
+ (vspace - (dr != null ? dr.mDrawableHeightRight : 0)) / 2;
return icontop + (dr != null ? dr.mDrawableHeightRight : 0)
protected boolean setFrame(int l, int t, int r, int b) { boolean result = super.setFrame(l, t, r, b);
Sets the list of input filters that will be used if the buffer is
Editable. Has no effect otherwise.
- Attr:
- ref android.R.styleable#TextView_maxLength
Sets the list of input filters on the specified Editable,
and includes mInput in the list if it is an InputFilter.
System.arraycopy(filters, 0, nf, 0, filters.length);
Returns the current list of input filters.
voffset = boxht - textht;
voffset = (boxht - textht) >> 1;
voffset = boxht - textht;
voffset = (boxht - textht) >> 1;
if (a >= 0 || b >= 0 || c >= 0) { int first = Math.min(Math.min(a, b), c);
int last = Math.max(Math.max(a, b), c);
if (drawable == drawables.mDrawableLeft) { final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2;
} else if (drawable == drawables.mDrawableRight) { final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2;
} else if (drawable == drawables.mDrawableTop) { final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2;
} else if (drawable == drawables.mDrawableBottom) { final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2;
invalidate(dirty.left + scrollX, dirty.top + scrollY,
dirty.right + scrollX, dirty.bottom + scrollY);
int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;
if (dr.mDrawableLeft != null) { scrollY + compoundPaddingTop +
(vspace - dr.mDrawableHeightLeft) / 2);
dr.mDrawableLeft.draw(canvas);
if (dr.mDrawableRight != null) { scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2);
dr.mDrawableRight.draw(canvas);
if (dr.mDrawableTop != null) { canvas.translate(scrollX + compoundPaddingLeft + (hspace - dr.mDrawableWidthTop) / 2,
dr.mDrawableTop.draw(canvas);
if (dr.mDrawableBottom != null) { canvas.translate(scrollX + compoundPaddingLeft +
(hspace - dr.mDrawableWidthBottom) / 2,
dr.mDrawableBottom.draw(canvas);
float clipLeft = compoundPaddingLeft + scrollX;
float clipTop = extendedPaddingTop + scrollY;
float clipRight = right - left - compoundPaddingRight + scrollX;
float clipBottom = bottom - top - extendedPaddingBottom + scrollY;
canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText);
int selStart = -1, selEnd = -1;
if (selStart == selEnd) { if (ims != null && ims.mBatchEditNesting == 0) { boolean reported = false;
if (ims.mContentChanged || ims.mSelectionModeChanged) { if (!reported && highlight != null) { ims.mTmpOffset[0] = ims.mTmpOffset[1] = 0;
ims.mTmpRectF.offset(ims.mTmpOffset[0], ims.mTmpOffset[1]);
ims.mTmpRectF.offset(0, voffsetCursor - voffsetText);
ims.mCursorRectInWindow.set((int)(ims.mTmpRectF.left + 0.5),
(int)(ims.mTmpRectF.top + 0.5),
(int)(ims.mTmpRectF.right + 0.5),
(int)(ims.mTmpRectF.bottom + 0.5));
ims.mCursorRectInWindow.left, ims.mCursorRectInWindow.top,
ims.mCursorRectInWindow.right, ims.mCursorRectInWindow.bottom);
Update the positions of the CursorControllers. Needed by WebTextView,
which does not draw.
r.offset(paddingLeft, paddingTop);
Return the number of lines of text, or 0 if the internal Layout has not
been built.
Return the baseline for the specified line (0...getLineCount() - 1)
If bounds is not null, return the top, left, right, bottom extents
of the specified line in it. If the internal Layout has not been built,
return 0 and set bounds to (0, 0, 0, 0)
- Parameters:
line which line to examine (0..getLineCount() - 1)bounds Optional. If not null, it returns the extent of the line- Returns:
- the Y-coordinate of the baseline
return baseline + voffset;
while (--repeatCount > 0) { while (--repeatCount > 0) { Returns true if pressing ENTER in this field advances focus instead
of inserting the character. This is true mostly in single-line fields,
but also in mail addresses and subjects which will display on multiple
lines but where it doesn't make sense to insert newlines.
if (otherEvent != null) { if (otherEvent != null) { return super.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
"that wasn't able to take focus!");
return super.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
outAttrs.hintText = mHint;
If this TextView contains editable content, extract a portion of it
based on the information in
request in to
outText.
- Returns:
- Returns true if the text was successfully extracted, else false.
int partialStartOffset, int partialEndOffset, int delta,
final int N = content.length();
if (partialStartOffset < 0) { outText.partialStartOffset = outText.partialEndOffset = -1;
if (j < partialStartOffset) partialStartOffset = j;
if (j > partialEndOffset) partialEndOffset = j;
outText.partialStartOffset = partialStartOffset;
outText.partialEndOffset = partialEndOffset;
partialEndOffset += delta;
if (partialStartOffset > N) { } else if (partialStartOffset < 0) { if (partialEndOffset > N) { } else if (partialEndOffset < 0) { outText.text = TextUtils.substring(content, partialStartOffset,
outText.partialStartOffset = 0;
outText.partialEndOffset = 0;
final boolean contentChanged = ims.mContentChanged;
if (contentChanged || ims.mSelectionModeChanged) { ims.mContentChanged = false;
ims.mSelectionModeChanged = false;
+ ims.mChangedStart + " end=" + ims.mChangedEnd
+ " delta=" + ims.mChangedDelta);
if (ims.mChangedStart < 0 && !contentChanged) { ims.mChangedDelta, ims.mTmpExtracted)) { + ims.mTmpExtracted.partialStartOffset
+ " end=" + ims.mTmpExtracted.partialEndOffset
+ ": " + ims.mTmpExtracted.text);
This is used to remove all style-impacting spans from text before new
extracted text is being replaced into it, so that we don't have any
lingering spans applied during the replace.
} else if (text.partialStartOffset < 0) { final int N = content.length();
int start = text.partialStartOffset;
if (start > N) start = N;
int end = text.partialEndOffset;
content.replace(start, end, text.text);
int start = text.selectionStart;
if (start < 0) start = 0;
else if (start > N) start = N;
int end = text.selectionEnd;
else if (end > N) end = N;
Called by the framework in response to a text completion from
the current input method, provided by it calling
InputConnection.commitCompletion(). The default implementation does
nothing; text views that are supporting auto-completion should override
this to do their desired behavior.
- Parameters:
text The auto complete text the user has selected.
int nesting = ++ims.mBatchEditNesting;
ims.mCursorChanged = false;
if (ims.mContentChanged) { ims.mContentChanged = false;
int nesting = --ims.mBatchEditNesting;
if (ims != null && ims.mBatchEditNesting != 0) { ims.mBatchEditNesting = 0;
if (ims.mContentChanged || ims.mSelectionModeChanged) { } else if (ims.mCursorChanged) { Called by the framework in response to a request to begin a batch
of edit operations through a call to link
beginBatchEdit().
Called by the framework in response to a request to end a batch
of edit operations through a call to link
endBatchEdit().
Called by the framework in response to a private command from the
current method, provided by it calling
InputConnection.performPrivateCommand().
- Parameters:
action The action name of the command.data Any additional data for the command. This may be null.- Returns:
- Return true if you handled the command, else false.
Make a new Layout based on the already-measured size of the view,
on the assumption that it was measured correctly at some point.
int physicalWidth = width;
The width passed in is now the desired layout width,
not the full view width with padding.
int ellipsisWidth, boolean bringIntoView) { (mEllipsize == null || boring.width <= ellipsisWidth)) { } else if (shouldEllipsize && boring.width <= w) { } else if (shouldEllipsize) { } else if (shouldEllipsize) { if (shouldEllipsize) hintWidth = w;
if (hintBoring != null) { if (hintBoring != null) { if (hintBoring.width <= hintWidth &&
(!shouldEllipsize || hintBoring.width <= ellipsisWidth)) { } else if (shouldEllipsize && hintBoring.width <= hintWidth) { } else if (shouldEllipsize) { } else if (shouldEllipsize) { final float overflow = (textWidth + 1.0f - width) / width;
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n; i++) { return (int) FloatMath.ceil(max);
Set whether the TextView includes extra top and bottom padding to make
room for accents that go above the normal ascent and descent.
The default is true.
- Attr:
- ref android.R.styleable#TextView_includeFontPadding
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
boolean fromexisting = false;
width = Math.max(width, dr.mDrawableWidthTop);
width = Math.max(width, dr.mDrawableWidthBottom);
if (hintBoring != null) { hintDes = (int) FloatMath.ceil(
hintWidth = hintBoring.width;
width = Math.min(widthSize, width);
int unpaddedWidth = want;
(fromexisting && des >= 0 && des <= want))) { height = Math.min(desired, heightSize);
desired = Math.max(desired, dr.mDrawableHeightLeft);
desired = Math.max(desired, dr.mDrawableHeightRight);
desired = Math.max(desired, dr.mDrawableHeightLeft);
desired = Math.max(desired, dr.mDrawableHeightRight);
Check whether a change to the existing text layout requires a
new view layout.
boolean sizeChanged = false;
Check whether entirely new text requires a new view layout
or merely a new text layout.
Returns true if anything changed.
if (right - left < hspace) { scrollx = (right + left) / 2 - hspace / 2;
scrollx = right - hspace;
scrollx = right - hspace;
scrollx = right - hspace;
Move the point, specified by the offset, into the view if it is needed.
This has to be called after layout. Returns true if anything changed.
int hslack = (bottom - top) / 2;
if (bottom - vs > vspace - vslack)
vs = bottom - (vspace - vslack);
if (x - hs > hspace - hslack) { hs = x - (hspace - hslack);
if (right - left <= hspace) { hs = left - (hspace - (right - left)) / 2;
} else if (x > right - hslack) { } else if (x < left + hslack) { } else if (right < hs + hspace) { if (x - hs > hspace - hslack) { hs = x - (hspace - hslack);
Rect r = new Rect(x, top, x + 1, bottom);
Move the cursor, if needed, so that it is at an offset that is visible
to the user. This will not move the cursor if it represents more than
one character (a selection range). This will only work if the
TextView contains spannable text; otherwise it will do nothing.
- Returns:
- True if the cursor was actually moved, false otherwise.
int vslack = (bottom - top) / 2;
} else if (bottom > (vspace+vs-vslack)) { if (newStart < leftChar) { } else if (newStart > rightChar) { r.left += horizontalOffset;
r.right += horizontalOffset;
r.bottom += verticalOffset;
public void debug(int depth) { output += "mText=\"" + mText + "\" ";
Return true iff there is a selection inside this text view.
return selectionStart >= 0 && selectionStart != selectionEnd;
Sets the properties of this field (lines, horizontally scrolling,
transformation method) to be for a single-line input.
- Attr:
- ref android.R.styleable#TextView_singleLine
If true, sets the properties of this field (lines, horizontally
scrolling, transformation method) to be for a single-line input;
if false, restores these to the default conditions.
Note that calling this with false restores default conditions,
not necessarily those that were in effect prior to calling
it with true.
- Attr:
- ref android.R.styleable#TextView_singleLine
private void applySingleLine(boolean singleLine, boolean applyTransformation) { if (applyTransformation) { if (applyTransformation) { Causes words in the text that are longer than the view is wide
to be ellipsized instead of broken in the middle. You may also
want to
setSingleLine() or
setHorizontallyScrolling(boolean)
to constrain the text to a single line. Use
null
to turn off ellipsizing.
- Attr:
- ref android.R.styleable#TextView_ellipsize
Sets how many times to repeat the marquee animation. Only applied if the
TextView has marquee enabled. Set to -1 to repeat indefinitely.
- Attr:
- ref android.R.styleable#TextView_marqueeRepeatLimit
Returns where, if anywhere, words that are longer than the view
is wide should be ellipsized.
Set the TextView so that when it takes focus, all the text is
selected.
- Attr:
- ref android.R.styleable#TextView_selectAllOnFocus
Set whether the cursor is visible. The default is true.
- Attr:
- ref android.R.styleable#TextView_cursorVisible
void start(int repeatLimit) { if (textView != null && textView.mLayout != null) { final float gap = textWidth / 3.0f;
This method is called when the text is changed, in case any
subclasses would like to know.
- Parameters:
text The text the TextView is displaying.start The offset of the start of the range of the text
that was modified.before The offset of the former end of the range of the
text that was modified. If text was simply inserted,
this will be the same as start.
If text was replaced with new text or deleted, the
length of the old text was before-start.after The offset of the end of the range of the text
that was modified. If text was simply deleted,
this will be the same as start.
If text was replaced with new text or inserted,
the length of the new text is after-start.
int start, int before, int after) { This method is called when the selection has changed, in case any
subclasses would like to know.
- Parameters:
selStart The new selection start location.selEnd The new selection end location.
Removes the specified TextWatcher from the list of those whose
methods are called
whenever this TextView's text changes.
final int count = list.size();
for (int i = 0; i < count; i++) { Not private so it can be called from an inner class without going
through a thunk.
final int count = list.size();
for (int i = 0; i < count; i++) { Not private so it can be called from an inner class without going
through a thunk.
final int count = list.size();
for (int i = 0; i < count; i++) { Not private so it can be called from an inner class without going
through a thunk.
if (ims == null || ims.mBatchEditNesting == 0) { ims.mContentChanged = true;
if (ims.mChangedStart < 0) { ims.mChangedStart = start;
ims.mChangedEnd = start+before;
if (ims.mChangedStart > start) ims.mChangedStart = start;
if (ims.mChangedEnd < (start+before)) ims.mChangedEnd = start+before;
ims.mChangedDelta += after-before;
Not private so it can be called from an inner class without going
through a thunk.
int oldEnd, int newEnd) { boolean selChanged = false;
int newSelStart=-1, newSelEnd=-1;
if (oldStart >= 0 || newStart >= 0) { if (oldStart >= 0 || newStart >= 0) { if (ims == null || ims.mBatchEditNesting == 0) { ims.mContentChanged = true;
ims.mSelectionModeChanged = true;
if (ims == null || ims.mBatchEditNesting == 0) { ims.mCursorChanged = true;
if (ims != null && ims.mExtracting != null) { if (ims.mBatchEditNesting != 0) { if (ims.mChangedStart > oldStart) { ims.mChangedStart = oldStart;
if (ims.mChangedStart > oldEnd) { ims.mChangedStart = oldEnd;
if (ims.mChangedStart > newStart) { ims.mChangedStart = newStart;
if (ims.mChangedStart > newEnd) { ims.mChangedStart = newEnd;
+ oldStart + "-" + oldEnd + ","
+ newStart + "-" + newEnd + what);
ims.mContentChanged = true;
+ " before=" + before + " after=" + after + ": " + buffer);
+ " before=" + before + " after=" + after + ": " + buffer);
Object what, int s, int e, int st, int en) { + " st=" + st + " en=" + en + " what=" + what + ": " + buf);
+ " what=" + what + ": " + buf);
+ " what=" + what + ": " + buf);
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if (lastTapPosition >= 0) { selStart >= 0 && selEnd >= 0) { if (lastTapPosition >= 0) { Log.e(LOG_TAG, "Invalid tap focus position (" + lastTapPosition + " vs " Use
BaseInputConnection.removeComposingSpans() to remove any IME composing
state from this text view.
if (start >= prevStart && start < prevEnd) { boolean windowSupportsHandles = false;
- Returns:
- True iff this TextView contains a text that can be edited.
Returns true, only while processing a touch gesture, if the initial
touch down event caused focus to move to the text view and as a result
its selection changed. Only valid while processing the touch gesture
of interest.
if (st == en && st >= 0 && en >= 0) { if (tv.mLayout != null) { NORMAL, SPANNABLE, EDITABLE,
Returns the TextView_textColor attribute from the
Resources.StyledAttributes, if set, or the TextAppearance_textColor
from the TextView_textAppearance attribute, if TextView_textColor
was not set directly.
Returns the default color from the TextView_textColor attribute
from the AttributeSet, if set, or the default color from the
TextAppearance_textColor from the TextView_textAppearance attribute,
if TextView_textColor was not set directly.
Returns the offsets delimiting the 'word' located at position offset.
- Parameters:
offset An offset in the text.- Returns:
- The offsets for the start and end of the word located at
offset.
The two ints offsets are packed in a long, with the starting offset shifted by 32 bits.
Returns a negative value if no valid word was found.
int end = Math.min(offset, len);
for (; start > 0; start--) {