2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.widget;
20 import android.annotation.ColorInt;
21 import android.annotation.DrawableRes;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.StringRes;
25 import android.annotation.StyleRes;
26 import android.annotation.XmlRes;
27 import android.app.Activity;
28 import android.app.assist.AssistStructure;
29 import android.content.ClipData;
30 import android.content.ClipboardManager;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.UndoManager;
34 import android.content.res.ColorStateList;
35 import android.content.res.CompatibilityInfo;
36 import android.content.res.Configuration;
37 import android.content.res.Resources;
38 import android.content.res.TypedArray;
39 import android.content.res.XmlResourceParser;
40 import android.graphics.Canvas;
41 import android.graphics.Insets;
42 import android.graphics.Paint;
43 import android.graphics.Path;
44 import android.graphics.PorterDuff;
45 import android.graphics.Rect;
46 import android.graphics.RectF;
47 import android.graphics.Typeface;
48 import android.graphics.drawable.Drawable;
49 import android.inputmethodservice.ExtractEditText;
50 import android.os.AsyncTask;
51 import android.os.Bundle;
52 import android.os.Parcel;
53 import android.os.Parcelable;
54 import android.os.ParcelableParcel;
55 import android.os.SystemClock;
56 import android.os.UserHandle;
57 import android.provider.Settings;
58 import android.text.BoringLayout;
59 import android.text.DynamicLayout;
60 import android.text.Editable;
61 import android.text.GetChars;
62 import android.text.GraphicsOperations;
63 import android.text.InputFilter;
64 import android.text.InputType;
65 import android.text.Layout;
66 import android.text.ParcelableSpan;
67 import android.text.Selection;
68 import android.text.SpanWatcher;
69 import android.text.Spannable;
70 import android.text.SpannableString;
71 import android.text.SpannableStringBuilder;
72 import android.text.Spanned;
73 import android.text.SpannedString;
74 import android.text.StaticLayout;
75 import android.text.TextDirectionHeuristic;
76 import android.text.TextDirectionHeuristics;
77 import android.text.TextPaint;
78 import android.text.TextUtils;
79 import android.text.TextUtils.TruncateAt;
80 import android.text.TextWatcher;
81 import android.text.method.AllCapsTransformationMethod;
82 import android.text.method.ArrowKeyMovementMethod;
83 import android.text.method.DateKeyListener;
84 import android.text.method.DateTimeKeyListener;
85 import android.text.method.DialerKeyListener;
86 import android.text.method.DigitsKeyListener;
87 import android.text.method.KeyListener;
88 import android.text.method.LinkMovementMethod;
89 import android.text.method.MetaKeyKeyListener;
90 import android.text.method.MovementMethod;
91 import android.text.method.PasswordTransformationMethod;
92 import android.text.method.SingleLineTransformationMethod;
93 import android.text.method.TextKeyListener;
94 import android.text.method.TimeKeyListener;
95 import android.text.method.TransformationMethod;
96 import android.text.method.TransformationMethod2;
97 import android.text.method.WordIterator;
98 import android.text.style.CharacterStyle;
99 import android.text.style.ClickableSpan;
100 import android.text.style.ParagraphStyle;
101 import android.text.style.SpellCheckSpan;
102 import android.text.style.SuggestionSpan;
103 import android.text.style.URLSpan;
104 import android.text.style.UpdateAppearance;
105 import android.text.util.Linkify;
106 import android.util.AttributeSet;
107 import android.util.Log;
108 import android.util.TypedValue;
109 import android.view.AccessibilityIterators.TextSegmentIterator;
110 import android.view.ActionMode;
111 import android.view.Choreographer;
112 import android.view.DragEvent;
113 import android.view.Gravity;
114 import android.view.HapticFeedbackConstants;
115 import android.view.KeyCharacterMap;
116 import android.view.KeyEvent;
117 import android.view.MotionEvent;
118 import android.view.View;
119 import android.view.ViewStructure;
120 import android.view.ViewConfiguration;
121 import android.view.ViewDebug;
122 import android.view.ViewGroup.LayoutParams;
123 import android.view.ViewRootImpl;
124 import android.view.ViewTreeObserver;
125 import android.view.ViewHierarchyEncoder;
126 import android.view.accessibility.AccessibilityEvent;
127 import android.view.accessibility.AccessibilityManager;
128 import android.view.accessibility.AccessibilityNodeInfo;
129 import android.view.animation.AnimationUtils;
130 import android.view.inputmethod.BaseInputConnection;
131 import android.view.inputmethod.CompletionInfo;
132 import android.view.inputmethod.CorrectionInfo;
133 import android.view.inputmethod.EditorInfo;
134 import android.view.inputmethod.ExtractedText;
135 import android.view.inputmethod.ExtractedTextRequest;
136 import android.view.inputmethod.InputConnection;
137 import android.view.inputmethod.InputMethodManager;
138 import android.view.textservice.SpellCheckerSubtype;
139 import android.view.textservice.TextServicesManager;
140 import android.widget.RemoteViews.RemoteView;
142 import com.android.internal.util.FastMath;
143 import com.android.internal.widget.EditableInputConnection;
145 import org.xmlpull.v1.XmlPullParserException;
147 import java.io.IOException;
148 import java.lang.ref.WeakReference;
149 import java.util.ArrayList;
150 import java.util.Locale;
152 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
155 * Displays text to the user and optionally allows them to edit it. A TextView
156 * is a complete text editor, however the basic class is configured to not
157 * allow editing; see {@link EditText} for a subclass that configures the text
161 * To allow users to copy some or all of the TextView's value and paste it somewhere else, set the
162 * XML attribute {@link android.R.styleable#TextView_textIsSelectable
163 * android:textIsSelectable} to "true" or call
164 * {@link #setTextIsSelectable setTextIsSelectable(true)}. The {@code textIsSelectable} flag
165 * allows users to make selection gestures in the TextView, which in turn triggers the system's
166 * built-in copy/paste controls.
168 * <b>XML attributes</b>
170 * See {@link android.R.styleable#TextView TextView Attributes},
171 * {@link android.R.styleable#View View Attributes}
173 * @attr ref android.R.styleable#TextView_text
174 * @attr ref android.R.styleable#TextView_bufferType
175 * @attr ref android.R.styleable#TextView_hint
176 * @attr ref android.R.styleable#TextView_textColor
177 * @attr ref android.R.styleable#TextView_textColorHighlight
178 * @attr ref android.R.styleable#TextView_textColorHint
179 * @attr ref android.R.styleable#TextView_textAppearance
180 * @attr ref android.R.styleable#TextView_textColorLink
181 * @attr ref android.R.styleable#TextView_textSize
182 * @attr ref android.R.styleable#TextView_textScaleX
183 * @attr ref android.R.styleable#TextView_fontFamily
184 * @attr ref android.R.styleable#TextView_typeface
185 * @attr ref android.R.styleable#TextView_textStyle
186 * @attr ref android.R.styleable#TextView_cursorVisible
187 * @attr ref android.R.styleable#TextView_maxLines
188 * @attr ref android.R.styleable#TextView_maxHeight
189 * @attr ref android.R.styleable#TextView_lines
190 * @attr ref android.R.styleable#TextView_height
191 * @attr ref android.R.styleable#TextView_minLines
192 * @attr ref android.R.styleable#TextView_minHeight
193 * @attr ref android.R.styleable#TextView_maxEms
194 * @attr ref android.R.styleable#TextView_maxWidth
195 * @attr ref android.R.styleable#TextView_ems
196 * @attr ref android.R.styleable#TextView_width
197 * @attr ref android.R.styleable#TextView_minEms
198 * @attr ref android.R.styleable#TextView_minWidth
199 * @attr ref android.R.styleable#TextView_gravity
200 * @attr ref android.R.styleable#TextView_scrollHorizontally
201 * @attr ref android.R.styleable#TextView_password
202 * @attr ref android.R.styleable#TextView_singleLine
203 * @attr ref android.R.styleable#TextView_selectAllOnFocus
204 * @attr ref android.R.styleable#TextView_includeFontPadding
205 * @attr ref android.R.styleable#TextView_maxLength
206 * @attr ref android.R.styleable#TextView_shadowColor
207 * @attr ref android.R.styleable#TextView_shadowDx
208 * @attr ref android.R.styleable#TextView_shadowDy
209 * @attr ref android.R.styleable#TextView_shadowRadius
210 * @attr ref android.R.styleable#TextView_autoLink
211 * @attr ref android.R.styleable#TextView_linksClickable
212 * @attr ref android.R.styleable#TextView_numeric
213 * @attr ref android.R.styleable#TextView_digits
214 * @attr ref android.R.styleable#TextView_phoneNumber
215 * @attr ref android.R.styleable#TextView_inputMethod
216 * @attr ref android.R.styleable#TextView_capitalize
217 * @attr ref android.R.styleable#TextView_autoText
218 * @attr ref android.R.styleable#TextView_editable
219 * @attr ref android.R.styleable#TextView_freezesText
220 * @attr ref android.R.styleable#TextView_ellipsize
221 * @attr ref android.R.styleable#TextView_drawableTop
222 * @attr ref android.R.styleable#TextView_drawableBottom
223 * @attr ref android.R.styleable#TextView_drawableRight
224 * @attr ref android.R.styleable#TextView_drawableLeft
225 * @attr ref android.R.styleable#TextView_drawableStart
226 * @attr ref android.R.styleable#TextView_drawableEnd
227 * @attr ref android.R.styleable#TextView_drawablePadding
228 * @attr ref android.R.styleable#TextView_drawableTint
229 * @attr ref android.R.styleable#TextView_drawableTintMode
230 * @attr ref android.R.styleable#TextView_lineSpacingExtra
231 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
232 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
233 * @attr ref android.R.styleable#TextView_inputType
234 * @attr ref android.R.styleable#TextView_imeOptions
235 * @attr ref android.R.styleable#TextView_privateImeOptions
236 * @attr ref android.R.styleable#TextView_imeActionLabel
237 * @attr ref android.R.styleable#TextView_imeActionId
238 * @attr ref android.R.styleable#TextView_editorExtras
239 * @attr ref android.R.styleable#TextView_elegantTextHeight
240 * @attr ref android.R.styleable#TextView_letterSpacing
241 * @attr ref android.R.styleable#TextView_fontFeatureSettings
242 * @attr ref android.R.styleable#TextView_breakStrategy
243 * @attr ref android.R.styleable#TextView_hyphenationFrequency
246 public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {
247 static final String LOG_TAG = "TextView";
248 static final boolean DEBUG_EXTRACT = false;
250 // Enum for the "typeface" XML parameter.
251 // TODO: How can we get this from the XML instead of hardcoding it here?
252 private static final int SANS = 1;
253 private static final int SERIF = 2;
254 private static final int MONOSPACE = 3;
256 // Bitfield for the "numeric" XML parameter.
257 // TODO: How can we get this from the XML instead of hardcoding it here?
258 private static final int SIGNED = 2;
259 private static final int DECIMAL = 4;
262 * Draw marquee text with fading edges as usual
264 private static final int MARQUEE_FADE_NORMAL = 0;
267 * Draw marquee text as ellipsize end while inactive instead of with the fade.
268 * (Useful for devices where the fade can be expensive if overdone)
270 private static final int MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS = 1;
273 * Draw marquee text with fading edges because it is currently active/animating.
275 private static final int MARQUEE_FADE_SWITCH_SHOW_FADE = 2;
277 private static final int LINES = 1;
278 private static final int EMS = LINES;
279 private static final int PIXELS = 2;
281 private static final RectF TEMP_RECTF = new RectF();
283 // XXX should be much larger
284 private static final int VERY_WIDE = 1024*1024;
285 private static final int ANIMATED_SCROLL_GAP = 250;
287 private static final InputFilter[] NO_FILTERS = new InputFilter[0];
288 private static final Spanned EMPTY_SPANNED = new SpannedString("");
290 private static final int CHANGE_WATCHER_PRIORITY = 100;
292 // New state used to change background based on whether this TextView is multiline.
293 private static final int[] MULTILINE_STATE_SET = { R.attr.state_multiline };
295 // System wide time for last cut, copy or text changed action.
296 static long sLastCutCopyOrTextChangedTime;
301 static final int PROCESS_TEXT_REQUEST_CODE = 100;
303 private ColorStateList mTextColor;
304 private ColorStateList mHintTextColor;
305 private ColorStateList mLinkTextColor;
306 @ViewDebug.ExportedProperty(category = "text")
307 private int mCurTextColor;
308 private int mCurHintTextColor;
309 private boolean mFreezesText;
310 private boolean mDispatchTemporaryDetach;
312 /** Whether this view is temporarily detached from the parent view. */
313 boolean mTemporaryDetach;
315 private Editable.Factory mEditableFactory = Editable.Factory.getInstance();
316 private Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance();
318 private float mShadowRadius, mShadowDx, mShadowDy;
319 private int mShadowColor;
321 private boolean mPreDrawRegistered;
322 private boolean mPreDrawListenerDetached;
324 // A flag to prevent repeated movements from escaping the enclosing text view. The idea here is
325 // that if a user is holding down a movement key to traverse text, we shouldn't also traverse
326 // the view hierarchy. On the other hand, if the user is using the movement key to traverse views
327 // (i.e. the first movement was to traverse out of this view, or this view was traversed into by
328 // the user holding the movement key down) then we shouldn't prevent the focus from changing.
329 private boolean mPreventDefaultMovement;
331 private TextUtils.TruncateAt mEllipsize;
333 static class Drawables {
334 static final int LEFT = 0;
335 static final int TOP = 1;
336 static final int RIGHT = 2;
337 static final int BOTTOM = 3;
339 static final int DRAWABLE_NONE = -1;
340 static final int DRAWABLE_RIGHT = 0;
341 static final int DRAWABLE_LEFT = 1;
343 final Rect mCompoundRect = new Rect();
345 final Drawable[] mShowing = new Drawable[4];
347 ColorStateList mTintList;
348 PorterDuff.Mode mTintMode;
350 boolean mHasTintMode;
352 Drawable mDrawableStart, mDrawableEnd, mDrawableError, mDrawableTemp;
353 Drawable mDrawableLeftInitial, mDrawableRightInitial;
355 boolean mIsRtlCompatibilityMode;
358 int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight,
359 mDrawableSizeStart, mDrawableSizeEnd, mDrawableSizeError, mDrawableSizeTemp;
361 int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight,
362 mDrawableHeightStart, mDrawableHeightEnd, mDrawableHeightError, mDrawableHeightTemp;
364 int mDrawablePadding;
366 int mDrawableSaved = DRAWABLE_NONE;
368 public Drawables(Context context) {
369 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
370 mIsRtlCompatibilityMode = (targetSdkVersion < JELLY_BEAN_MR1 ||
371 !context.getApplicationInfo().hasRtlSupport());
375 public void resolveWithLayoutDirection(int layoutDirection) {
376 // First reset "left" and "right" drawables to their initial values
377 mShowing[Drawables.LEFT] = mDrawableLeftInitial;
378 mShowing[Drawables.RIGHT] = mDrawableRightInitial;
380 if (mIsRtlCompatibilityMode) {
381 // Use "start" drawable as "left" drawable if the "left" drawable was not defined
382 if (mDrawableStart != null && mShowing[Drawables.LEFT] == null) {
383 mShowing[Drawables.LEFT] = mDrawableStart;
384 mDrawableSizeLeft = mDrawableSizeStart;
385 mDrawableHeightLeft = mDrawableHeightStart;
387 // Use "end" drawable as "right" drawable if the "right" drawable was not defined
388 if (mDrawableEnd != null && mShowing[Drawables.RIGHT] == null) {
389 mShowing[Drawables.RIGHT] = mDrawableEnd;
390 mDrawableSizeRight = mDrawableSizeEnd;
391 mDrawableHeightRight = mDrawableHeightEnd;
394 // JB-MR1+ normal case: "start" / "end" drawables are overriding "left" / "right"
395 // drawable if and only if they have been defined
396 switch(layoutDirection) {
397 case LAYOUT_DIRECTION_RTL:
399 mShowing[Drawables.RIGHT] = mDrawableStart;
400 mDrawableSizeRight = mDrawableSizeStart;
401 mDrawableHeightRight = mDrawableHeightStart;
403 mShowing[Drawables.LEFT] = mDrawableEnd;
404 mDrawableSizeLeft = mDrawableSizeEnd;
405 mDrawableHeightLeft = mDrawableHeightEnd;
409 case LAYOUT_DIRECTION_LTR:
412 mShowing[Drawables.LEFT] = mDrawableStart;
413 mDrawableSizeLeft = mDrawableSizeStart;
414 mDrawableHeightLeft = mDrawableHeightStart;
416 mShowing[Drawables.RIGHT] = mDrawableEnd;
417 mDrawableSizeRight = mDrawableSizeEnd;
418 mDrawableHeightRight = mDrawableHeightEnd;
423 applyErrorDrawableIfNeeded(layoutDirection);
424 updateDrawablesLayoutDirection(layoutDirection);
427 private void updateDrawablesLayoutDirection(int layoutDirection) {
428 for (Drawable dr : mShowing) {
430 dr.setLayoutDirection(layoutDirection);
435 public void setErrorDrawable(Drawable dr, TextView tv) {
436 if (mDrawableError != dr && mDrawableError != null) {
437 mDrawableError.setCallback(null);
441 if (mDrawableError != null) {
442 final Rect compoundRect = mCompoundRect;
443 final int[] state = tv.getDrawableState();
445 mDrawableError.setState(state);
446 mDrawableError.copyBounds(compoundRect);
447 mDrawableError.setCallback(tv);
448 mDrawableSizeError = compoundRect.width();
449 mDrawableHeightError = compoundRect.height();
451 mDrawableSizeError = mDrawableHeightError = 0;
455 private void applyErrorDrawableIfNeeded(int layoutDirection) {
456 // first restore the initial state if needed
457 switch (mDrawableSaved) {
459 mShowing[Drawables.LEFT] = mDrawableTemp;
460 mDrawableSizeLeft = mDrawableSizeTemp;
461 mDrawableHeightLeft = mDrawableHeightTemp;
464 mShowing[Drawables.RIGHT] = mDrawableTemp;
465 mDrawableSizeRight = mDrawableSizeTemp;
466 mDrawableHeightRight = mDrawableHeightTemp;
471 // then, if needed, assign the Error drawable to the correct location
472 if (mDrawableError != null) {
473 switch(layoutDirection) {
474 case LAYOUT_DIRECTION_RTL:
475 mDrawableSaved = DRAWABLE_LEFT;
477 mDrawableTemp = mShowing[Drawables.LEFT];
478 mDrawableSizeTemp = mDrawableSizeLeft;
479 mDrawableHeightTemp = mDrawableHeightLeft;
481 mShowing[Drawables.LEFT] = mDrawableError;
482 mDrawableSizeLeft = mDrawableSizeError;
483 mDrawableHeightLeft = mDrawableHeightError;
485 case LAYOUT_DIRECTION_LTR:
487 mDrawableSaved = DRAWABLE_RIGHT;
489 mDrawableTemp = mShowing[Drawables.RIGHT];
490 mDrawableSizeTemp = mDrawableSizeRight;
491 mDrawableHeightTemp = mDrawableHeightRight;
493 mShowing[Drawables.RIGHT] = mDrawableError;
494 mDrawableSizeRight = mDrawableSizeError;
495 mDrawableHeightRight = mDrawableHeightError;
502 Drawables mDrawables;
504 private CharWrapper mCharWrapper;
506 private Marquee mMarquee;
507 private boolean mRestartMarquee;
509 private int mMarqueeRepeatLimit = 3;
511 private int mLastLayoutDirection = -1;
514 * On some devices the fading edges add a performance penalty if used
515 * extensively in the same layout. This mode indicates how the marquee
516 * is currently being shown, if applicable. (mEllipsize will == MARQUEE)
518 private int mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
521 * When mMarqueeFadeMode is not MARQUEE_FADE_NORMAL, this stores
522 * the layout that should be used when the mode switches.
524 private Layout mSavedMarqueeModeLayout;
526 @ViewDebug.ExportedProperty(category = "text")
527 private CharSequence mText;
528 private CharSequence mTransformed;
529 private BufferType mBufferType = BufferType.NORMAL;
531 private CharSequence mHint;
532 private Layout mHintLayout;
534 private MovementMethod mMovement;
536 private TransformationMethod mTransformation;
537 private boolean mAllowTransformationLengthChange;
538 private ChangeWatcher mChangeWatcher;
540 private ArrayList<TextWatcher> mListeners;
542 // display attributes
543 private final TextPaint mTextPaint;
544 private boolean mUserSetTextScaleX;
545 private Layout mLayout;
546 private boolean mLocaleChanged = false;
548 @ViewDebug.ExportedProperty(category = "text")
549 private int mGravity = Gravity.TOP | Gravity.START;
550 private boolean mHorizontallyScrolling;
552 private int mAutoLinkMask;
553 private boolean mLinksClickable = true;
555 private float mSpacingMult = 1.0f;
556 private float mSpacingAdd = 0.0f;
558 private int mBreakStrategy;
559 private int mHyphenationFrequency;
561 private int mMaximum = Integer.MAX_VALUE;
562 private int mMaxMode = LINES;
563 private int mMinimum = 0;
564 private int mMinMode = LINES;
566 private int mOldMaximum = mMaximum;
567 private int mOldMaxMode = mMaxMode;
569 private int mMaxWidth = Integer.MAX_VALUE;
570 private int mMaxWidthMode = PIXELS;
571 private int mMinWidth = 0;
572 private int mMinWidthMode = PIXELS;
574 private boolean mSingleLine;
575 private int mDesiredHeightAtMeasure = -1;
576 private boolean mIncludePad = true;
577 private int mDeferScroll = -1;
579 // tmp primitives, so we don't alloc them on each draw
580 private Rect mTempRect;
581 private long mLastScroll;
582 private Scroller mScroller;
584 private BoringLayout.Metrics mBoring, mHintBoring;
585 private BoringLayout mSavedLayout, mSavedHintLayout;
587 private TextDirectionHeuristic mTextDir;
589 private InputFilter[] mFilters = NO_FILTERS;
591 private volatile Locale mCurrentSpellCheckerLocaleCache;
593 // It is possible to have a selection even when mEditor is null (programmatically set, like when
594 // a link is pressed). These highlight-related fields do not go in mEditor.
595 int mHighlightColor = 0x6633B5E5;
596 private Path mHighlightPath;
597 private final Paint mHighlightPaint;
598 private boolean mHighlightPathBogus = true;
600 private boolean mFirstTouch = false;
601 private long mLastTouchUpTime = 0;
603 // Although these fields are specific to editable text, they are not added to Editor because
604 // they are defined by the TextView's style and are theme-dependent.
605 int mCursorDrawableRes;
606 // These four fields, could be moved to Editor, since we know their default values and we
607 // could condition the creation of the Editor to a non standard value. This is however
608 // brittle since the hardcoded values here (such as
609 // com.android.internal.R.drawable.text_select_handle_left) would have to be updated if the
610 // default style is modified.
611 int mTextSelectHandleLeftRes;
612 int mTextSelectHandleRightRes;
613 int mTextSelectHandleRes;
614 int mTextEditSuggestionItemLayout;
617 * EditText specific data, created on demand when one of the Editor fields is used.
618 * See {@link #createEditorIfNeeded()}.
620 private Editor mEditor;
623 * Kick-start the font cache for the zygote process (to pay the cost of
624 * initializing freetype for our default font only once).
627 Paint p = new Paint();
628 p.setAntiAlias(true);
629 // We don't care about the result, just the side-effect of measuring.
634 * Interface definition for a callback to be invoked when an action is
635 * performed on the editor.
637 public interface OnEditorActionListener {
639 * Called when an action is being performed.
641 * @param v The view that was clicked.
642 * @param actionId Identifier of the action. This will be either the
643 * identifier you supplied, or {@link EditorInfo#IME_NULL
644 * EditorInfo.IME_NULL} if being called due to the enter key
646 * @param event If triggered by an enter key, this is the event;
647 * otherwise, this is null.
648 * @return Return true if you have consumed the action, else false.
650 boolean onEditorAction(TextView v, int actionId, KeyEvent event);
653 public TextView(Context context) {
657 public TextView(Context context, @Nullable AttributeSet attrs) {
658 this(context, attrs, com.android.internal.R.attr.textViewStyle);
661 public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
662 this(context, attrs, defStyleAttr, 0);
665 @SuppressWarnings("deprecation")
667 Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
668 super(context, attrs, defStyleAttr, defStyleRes);
672 final Resources res = getResources();
673 final CompatibilityInfo compat = res.getCompatibilityInfo();
675 mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
676 mTextPaint.density = res.getDisplayMetrics().density;
677 mTextPaint.setCompatibilityScaling(compat.applicationScale);
679 mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
680 mHighlightPaint.setCompatibilityScaling(compat.applicationScale);
682 mMovement = getDefaultMovementMethod();
684 mTransformation = null;
686 int textColorHighlight = 0;
687 ColorStateList textColor = null;
688 ColorStateList textColorHint = null;
689 ColorStateList textColorLink = null;
691 String fontFamily = null;
692 boolean fontFamilyExplicit = false;
693 int typefaceIndex = -1;
695 boolean allCaps = false;
697 float dx = 0, dy = 0, r = 0;
698 boolean elegant = false;
699 float letterSpacing = 0;
700 String fontFeatureSettings = null;
701 mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE;
702 mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE;
704 final Resources.Theme theme = context.getTheme();
707 * Look the appearance up without checking first if it exists because
708 * almost every TextView has one and it greatly simplifies the logic
709 * to be able to parse the appearance first and then let specific tags
710 * for this View override it.
712 TypedArray a = theme.obtainStyledAttributes(attrs,
713 com.android.internal.R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);
714 TypedArray appearance = null;
715 int ap = a.getResourceId(
716 com.android.internal.R.styleable.TextViewAppearance_textAppearance, -1);
719 appearance = theme.obtainStyledAttributes(
720 ap, com.android.internal.R.styleable.TextAppearance);
722 if (appearance != null) {
723 int n = appearance.getIndexCount();
724 for (int i = 0; i < n; i++) {
725 int attr = appearance.getIndex(i);
728 case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
729 textColorHighlight = appearance.getColor(attr, textColorHighlight);
732 case com.android.internal.R.styleable.TextAppearance_textColor:
733 textColor = appearance.getColorStateList(attr);
736 case com.android.internal.R.styleable.TextAppearance_textColorHint:
737 textColorHint = appearance.getColorStateList(attr);
740 case com.android.internal.R.styleable.TextAppearance_textColorLink:
741 textColorLink = appearance.getColorStateList(attr);
744 case com.android.internal.R.styleable.TextAppearance_textSize:
745 textSize = appearance.getDimensionPixelSize(attr, textSize);
748 case com.android.internal.R.styleable.TextAppearance_typeface:
749 typefaceIndex = appearance.getInt(attr, -1);
752 case com.android.internal.R.styleable.TextAppearance_fontFamily:
753 fontFamily = appearance.getString(attr);
756 case com.android.internal.R.styleable.TextAppearance_textStyle:
757 styleIndex = appearance.getInt(attr, -1);
760 case com.android.internal.R.styleable.TextAppearance_textAllCaps:
761 allCaps = appearance.getBoolean(attr, false);
764 case com.android.internal.R.styleable.TextAppearance_shadowColor:
765 shadowcolor = appearance.getInt(attr, 0);
768 case com.android.internal.R.styleable.TextAppearance_shadowDx:
769 dx = appearance.getFloat(attr, 0);
772 case com.android.internal.R.styleable.TextAppearance_shadowDy:
773 dy = appearance.getFloat(attr, 0);
776 case com.android.internal.R.styleable.TextAppearance_shadowRadius:
777 r = appearance.getFloat(attr, 0);
780 case com.android.internal.R.styleable.TextAppearance_elegantTextHeight:
781 elegant = appearance.getBoolean(attr, false);
784 case com.android.internal.R.styleable.TextAppearance_letterSpacing:
785 letterSpacing = appearance.getFloat(attr, 0);
788 case com.android.internal.R.styleable.TextAppearance_fontFeatureSettings:
789 fontFeatureSettings = appearance.getString(attr);
794 appearance.recycle();
797 boolean editable = getDefaultEditable();
798 CharSequence inputMethod = null;
800 CharSequence digits = null;
801 boolean phone = false;
802 boolean autotext = false;
805 boolean selectallonfocus = false;
806 Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
807 drawableBottom = null, drawableStart = null, drawableEnd = null;
808 ColorStateList drawableTint = null;
809 PorterDuff.Mode drawableTintMode = null;
810 int drawablePadding = 0;
812 boolean singleLine = false;
814 CharSequence text = "";
815 CharSequence hint = null;
816 boolean password = false;
817 int inputType = EditorInfo.TYPE_NULL;
819 a = theme.obtainStyledAttributes(
820 attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
822 int n = a.getIndexCount();
823 for (int i = 0; i < n; i++) {
824 int attr = a.getIndex(i);
827 case com.android.internal.R.styleable.TextView_editable:
828 editable = a.getBoolean(attr, editable);
831 case com.android.internal.R.styleable.TextView_inputMethod:
832 inputMethod = a.getText(attr);
835 case com.android.internal.R.styleable.TextView_numeric:
836 numeric = a.getInt(attr, numeric);
839 case com.android.internal.R.styleable.TextView_digits:
840 digits = a.getText(attr);
843 case com.android.internal.R.styleable.TextView_phoneNumber:
844 phone = a.getBoolean(attr, phone);
847 case com.android.internal.R.styleable.TextView_autoText:
848 autotext = a.getBoolean(attr, autotext);
851 case com.android.internal.R.styleable.TextView_capitalize:
852 autocap = a.getInt(attr, autocap);
855 case com.android.internal.R.styleable.TextView_bufferType:
856 buffertype = a.getInt(attr, buffertype);
859 case com.android.internal.R.styleable.TextView_selectAllOnFocus:
860 selectallonfocus = a.getBoolean(attr, selectallonfocus);
863 case com.android.internal.R.styleable.TextView_autoLink:
864 mAutoLinkMask = a.getInt(attr, 0);
867 case com.android.internal.R.styleable.TextView_linksClickable:
868 mLinksClickable = a.getBoolean(attr, true);
871 case com.android.internal.R.styleable.TextView_drawableLeft:
872 drawableLeft = a.getDrawable(attr);
875 case com.android.internal.R.styleable.TextView_drawableTop:
876 drawableTop = a.getDrawable(attr);
879 case com.android.internal.R.styleable.TextView_drawableRight:
880 drawableRight = a.getDrawable(attr);
883 case com.android.internal.R.styleable.TextView_drawableBottom:
884 drawableBottom = a.getDrawable(attr);
887 case com.android.internal.R.styleable.TextView_drawableStart:
888 drawableStart = a.getDrawable(attr);
891 case com.android.internal.R.styleable.TextView_drawableEnd:
892 drawableEnd = a.getDrawable(attr);
895 case com.android.internal.R.styleable.TextView_drawableTint:
896 drawableTint = a.getColorStateList(attr);
899 case com.android.internal.R.styleable.TextView_drawableTintMode:
900 drawableTintMode = Drawable.parseTintMode(a.getInt(attr, -1), drawableTintMode);
903 case com.android.internal.R.styleable.TextView_drawablePadding:
904 drawablePadding = a.getDimensionPixelSize(attr, drawablePadding);
907 case com.android.internal.R.styleable.TextView_maxLines:
908 setMaxLines(a.getInt(attr, -1));
911 case com.android.internal.R.styleable.TextView_maxHeight:
912 setMaxHeight(a.getDimensionPixelSize(attr, -1));
915 case com.android.internal.R.styleable.TextView_lines:
916 setLines(a.getInt(attr, -1));
919 case com.android.internal.R.styleable.TextView_height:
920 setHeight(a.getDimensionPixelSize(attr, -1));
923 case com.android.internal.R.styleable.TextView_minLines:
924 setMinLines(a.getInt(attr, -1));
927 case com.android.internal.R.styleable.TextView_minHeight:
928 setMinHeight(a.getDimensionPixelSize(attr, -1));
931 case com.android.internal.R.styleable.TextView_maxEms:
932 setMaxEms(a.getInt(attr, -1));
935 case com.android.internal.R.styleable.TextView_maxWidth:
936 setMaxWidth(a.getDimensionPixelSize(attr, -1));
939 case com.android.internal.R.styleable.TextView_ems:
940 setEms(a.getInt(attr, -1));
943 case com.android.internal.R.styleable.TextView_width:
944 setWidth(a.getDimensionPixelSize(attr, -1));
947 case com.android.internal.R.styleable.TextView_minEms:
948 setMinEms(a.getInt(attr, -1));
951 case com.android.internal.R.styleable.TextView_minWidth:
952 setMinWidth(a.getDimensionPixelSize(attr, -1));
955 case com.android.internal.R.styleable.TextView_gravity:
956 setGravity(a.getInt(attr, -1));
959 case com.android.internal.R.styleable.TextView_hint:
960 hint = a.getText(attr);
963 case com.android.internal.R.styleable.TextView_text:
964 text = a.getText(attr);
967 case com.android.internal.R.styleable.TextView_scrollHorizontally:
968 if (a.getBoolean(attr, false)) {
969 setHorizontallyScrolling(true);
973 case com.android.internal.R.styleable.TextView_singleLine:
974 singleLine = a.getBoolean(attr, singleLine);
977 case com.android.internal.R.styleable.TextView_ellipsize:
978 ellipsize = a.getInt(attr, ellipsize);
981 case com.android.internal.R.styleable.TextView_marqueeRepeatLimit:
982 setMarqueeRepeatLimit(a.getInt(attr, mMarqueeRepeatLimit));
985 case com.android.internal.R.styleable.TextView_includeFontPadding:
986 if (!a.getBoolean(attr, true)) {
987 setIncludeFontPadding(false);
991 case com.android.internal.R.styleable.TextView_cursorVisible:
992 if (!a.getBoolean(attr, true)) {
993 setCursorVisible(false);
997 case com.android.internal.R.styleable.TextView_maxLength:
998 maxlength = a.getInt(attr, -1);
1001 case com.android.internal.R.styleable.TextView_textScaleX:
1002 setTextScaleX(a.getFloat(attr, 1.0f));
1005 case com.android.internal.R.styleable.TextView_freezesText:
1006 mFreezesText = a.getBoolean(attr, false);
1009 case com.android.internal.R.styleable.TextView_shadowColor:
1010 shadowcolor = a.getInt(attr, 0);
1013 case com.android.internal.R.styleable.TextView_shadowDx:
1014 dx = a.getFloat(attr, 0);
1017 case com.android.internal.R.styleable.TextView_shadowDy:
1018 dy = a.getFloat(attr, 0);
1021 case com.android.internal.R.styleable.TextView_shadowRadius:
1022 r = a.getFloat(attr, 0);
1025 case com.android.internal.R.styleable.TextView_enabled:
1026 setEnabled(a.getBoolean(attr, isEnabled()));
1029 case com.android.internal.R.styleable.TextView_textColorHighlight:
1030 textColorHighlight = a.getColor(attr, textColorHighlight);
1033 case com.android.internal.R.styleable.TextView_textColor:
1034 textColor = a.getColorStateList(attr);
1037 case com.android.internal.R.styleable.TextView_textColorHint:
1038 textColorHint = a.getColorStateList(attr);
1041 case com.android.internal.R.styleable.TextView_textColorLink:
1042 textColorLink = a.getColorStateList(attr);
1045 case com.android.internal.R.styleable.TextView_textSize:
1046 textSize = a.getDimensionPixelSize(attr, textSize);
1049 case com.android.internal.R.styleable.TextView_typeface:
1050 typefaceIndex = a.getInt(attr, typefaceIndex);
1053 case com.android.internal.R.styleable.TextView_textStyle:
1054 styleIndex = a.getInt(attr, styleIndex);
1057 case com.android.internal.R.styleable.TextView_fontFamily:
1058 fontFamily = a.getString(attr);
1059 fontFamilyExplicit = true;
1062 case com.android.internal.R.styleable.TextView_password:
1063 password = a.getBoolean(attr, password);
1066 case com.android.internal.R.styleable.TextView_lineSpacingExtra:
1067 mSpacingAdd = a.getDimensionPixelSize(attr, (int) mSpacingAdd);
1070 case com.android.internal.R.styleable.TextView_lineSpacingMultiplier:
1071 mSpacingMult = a.getFloat(attr, mSpacingMult);
1074 case com.android.internal.R.styleable.TextView_inputType:
1075 inputType = a.getInt(attr, EditorInfo.TYPE_NULL);
1078 case com.android.internal.R.styleable.TextView_allowUndo:
1079 createEditorIfNeeded();
1080 mEditor.mAllowUndo = a.getBoolean(attr, true);
1083 case com.android.internal.R.styleable.TextView_imeOptions:
1084 createEditorIfNeeded();
1085 mEditor.createInputContentTypeIfNeeded();
1086 mEditor.mInputContentType.imeOptions = a.getInt(attr,
1087 mEditor.mInputContentType.imeOptions);
1090 case com.android.internal.R.styleable.TextView_imeActionLabel:
1091 createEditorIfNeeded();
1092 mEditor.createInputContentTypeIfNeeded();
1093 mEditor.mInputContentType.imeActionLabel = a.getText(attr);
1096 case com.android.internal.R.styleable.TextView_imeActionId:
1097 createEditorIfNeeded();
1098 mEditor.createInputContentTypeIfNeeded();
1099 mEditor.mInputContentType.imeActionId = a.getInt(attr,
1100 mEditor.mInputContentType.imeActionId);
1103 case com.android.internal.R.styleable.TextView_privateImeOptions:
1104 setPrivateImeOptions(a.getString(attr));
1107 case com.android.internal.R.styleable.TextView_editorExtras:
1109 setInputExtras(a.getResourceId(attr, 0));
1110 } catch (XmlPullParserException e) {
1111 Log.w(LOG_TAG, "Failure reading input extras", e);
1112 } catch (IOException e) {
1113 Log.w(LOG_TAG, "Failure reading input extras", e);
1117 case com.android.internal.R.styleable.TextView_textCursorDrawable:
1118 mCursorDrawableRes = a.getResourceId(attr, 0);
1121 case com.android.internal.R.styleable.TextView_textSelectHandleLeft:
1122 mTextSelectHandleLeftRes = a.getResourceId(attr, 0);
1125 case com.android.internal.R.styleable.TextView_textSelectHandleRight:
1126 mTextSelectHandleRightRes = a.getResourceId(attr, 0);
1129 case com.android.internal.R.styleable.TextView_textSelectHandle:
1130 mTextSelectHandleRes = a.getResourceId(attr, 0);
1133 case com.android.internal.R.styleable.TextView_textEditSuggestionItemLayout:
1134 mTextEditSuggestionItemLayout = a.getResourceId(attr, 0);
1137 case com.android.internal.R.styleable.TextView_textIsSelectable:
1138 setTextIsSelectable(a.getBoolean(attr, false));
1141 case com.android.internal.R.styleable.TextView_textAllCaps:
1142 allCaps = a.getBoolean(attr, false);
1145 case com.android.internal.R.styleable.TextView_elegantTextHeight:
1146 elegant = a.getBoolean(attr, false);
1149 case com.android.internal.R.styleable.TextView_letterSpacing:
1150 letterSpacing = a.getFloat(attr, 0);
1153 case com.android.internal.R.styleable.TextView_fontFeatureSettings:
1154 fontFeatureSettings = a.getString(attr);
1157 case com.android.internal.R.styleable.TextView_breakStrategy:
1158 mBreakStrategy = a.getInt(attr, Layout.BREAK_STRATEGY_SIMPLE);
1161 case com.android.internal.R.styleable.TextView_hyphenationFrequency:
1162 mHyphenationFrequency = a.getInt(attr, Layout.HYPHENATION_FREQUENCY_NONE);
1168 BufferType bufferType = BufferType.EDITABLE;
1170 final int variation =
1171 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
1172 final boolean passwordInputType = variation
1173 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
1174 final boolean webPasswordInputType = variation
1175 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD);
1176 final boolean numberPasswordInputType = variation
1177 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
1179 if (inputMethod != null) {
1183 c = Class.forName(inputMethod.toString());
1184 } catch (ClassNotFoundException ex) {
1185 throw new RuntimeException(ex);
1189 createEditorIfNeeded();
1190 mEditor.mKeyListener = (KeyListener) c.newInstance();
1191 } catch (InstantiationException ex) {
1192 throw new RuntimeException(ex);
1193 } catch (IllegalAccessException ex) {
1194 throw new RuntimeException(ex);
1197 mEditor.mInputType = inputType != EditorInfo.TYPE_NULL
1199 : mEditor.mKeyListener.getInputType();
1200 } catch (IncompatibleClassChangeError e) {
1201 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT;
1203 } else if (digits != null) {
1204 createEditorIfNeeded();
1205 mEditor.mKeyListener = DigitsKeyListener.getInstance(digits.toString());
1206 // If no input type was specified, we will default to generic
1207 // text, since we can't tell the IME about the set of digits
1208 // that was selected.
1209 mEditor.mInputType = inputType != EditorInfo.TYPE_NULL
1210 ? inputType : EditorInfo.TYPE_CLASS_TEXT;
1211 } else if (inputType != EditorInfo.TYPE_NULL) {
1212 setInputType(inputType, true);
1213 // If set, the input type overrides what was set using the deprecated singleLine flag.
1214 singleLine = !isMultilineInputType(inputType);
1216 createEditorIfNeeded();
1217 mEditor.mKeyListener = DialerKeyListener.getInstance();
1218 mEditor.mInputType = inputType = EditorInfo.TYPE_CLASS_PHONE;
1219 } else if (numeric != 0) {
1220 createEditorIfNeeded();
1221 mEditor.mKeyListener = DigitsKeyListener.getInstance((numeric & SIGNED) != 0,
1222 (numeric & DECIMAL) != 0);
1223 inputType = EditorInfo.TYPE_CLASS_NUMBER;
1224 if ((numeric & SIGNED) != 0) {
1225 inputType |= EditorInfo.TYPE_NUMBER_FLAG_SIGNED;
1227 if ((numeric & DECIMAL) != 0) {
1228 inputType |= EditorInfo.TYPE_NUMBER_FLAG_DECIMAL;
1230 mEditor.mInputType = inputType;
1231 } else if (autotext || autocap != -1) {
1232 TextKeyListener.Capitalize cap;
1234 inputType = EditorInfo.TYPE_CLASS_TEXT;
1238 cap = TextKeyListener.Capitalize.SENTENCES;
1239 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES;
1243 cap = TextKeyListener.Capitalize.WORDS;
1244 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
1248 cap = TextKeyListener.Capitalize.CHARACTERS;
1249 inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS;
1253 cap = TextKeyListener.Capitalize.NONE;
1257 createEditorIfNeeded();
1258 mEditor.mKeyListener = TextKeyListener.getInstance(autotext, cap);
1259 mEditor.mInputType = inputType;
1260 } else if (isTextSelectable()) {
1261 // Prevent text changes from keyboard.
1262 if (mEditor != null) {
1263 mEditor.mKeyListener = null;
1264 mEditor.mInputType = EditorInfo.TYPE_NULL;
1266 bufferType = BufferType.SPANNABLE;
1267 // So that selection can be changed using arrow keys and touch is handled.
1268 setMovementMethod(ArrowKeyMovementMethod.getInstance());
1269 } else if (editable) {
1270 createEditorIfNeeded();
1271 mEditor.mKeyListener = TextKeyListener.getInstance();
1272 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT;
1274 if (mEditor != null) mEditor.mKeyListener = null;
1276 switch (buffertype) {
1278 bufferType = BufferType.NORMAL;
1281 bufferType = BufferType.SPANNABLE;
1284 bufferType = BufferType.EDITABLE;
1289 if (mEditor != null) mEditor.adjustInputType(password, passwordInputType,
1290 webPasswordInputType, numberPasswordInputType);
1292 if (selectallonfocus) {
1293 createEditorIfNeeded();
1294 mEditor.mSelectAllOnFocus = true;
1296 if (bufferType == BufferType.NORMAL)
1297 bufferType = BufferType.SPANNABLE;
1300 // Set up the tint (if needed) before setting the drawables so that it
1301 // gets applied correctly.
1302 if (drawableTint != null || drawableTintMode != null) {
1303 if (mDrawables == null) {
1304 mDrawables = new Drawables(context);
1306 if (drawableTint != null) {
1307 mDrawables.mTintList = drawableTint;
1308 mDrawables.mHasTint = true;
1310 if (drawableTintMode != null) {
1311 mDrawables.mTintMode = drawableTintMode;
1312 mDrawables.mHasTintMode = true;
1316 // This call will save the initial left/right drawables
1317 setCompoundDrawablesWithIntrinsicBounds(
1318 drawableLeft, drawableTop, drawableRight, drawableBottom);
1319 setRelativeDrawablesIfNeeded(drawableStart, drawableEnd);
1320 setCompoundDrawablePadding(drawablePadding);
1322 // Same as setSingleLine(), but make sure the transformation method and the maximum number
1323 // of lines of height are unchanged for multi-line TextViews.
1324 setInputTypeSingleLine(singleLine);
1325 applySingleLine(singleLine, singleLine, singleLine);
1327 if (singleLine && getKeyListener() == null && ellipsize < 0) {
1328 ellipsize = 3; // END
1331 switch (ellipsize) {
1333 setEllipsize(TextUtils.TruncateAt.START);
1336 setEllipsize(TextUtils.TruncateAt.MIDDLE);
1339 setEllipsize(TextUtils.TruncateAt.END);
1342 if (ViewConfiguration.get(context).isFadingMarqueeEnabled()) {
1343 setHorizontalFadingEdgeEnabled(true);
1344 mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
1346 setHorizontalFadingEdgeEnabled(false);
1347 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
1349 setEllipsize(TextUtils.TruncateAt.MARQUEE);
1353 setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000));
1354 setHintTextColor(textColorHint);
1355 setLinkTextColor(textColorLink);
1356 if (textColorHighlight != 0) {
1357 setHighlightColor(textColorHighlight);
1359 setRawTextSize(textSize);
1360 setElegantTextHeight(elegant);
1361 setLetterSpacing(letterSpacing);
1362 setFontFeatureSettings(fontFeatureSettings);
1365 setTransformationMethod(new AllCapsTransformationMethod(getContext()));
1368 if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) {
1369 setTransformationMethod(PasswordTransformationMethod.getInstance());
1370 typefaceIndex = MONOSPACE;
1371 } else if (mEditor != null &&
1372 (mEditor.mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION))
1373 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
1374 typefaceIndex = MONOSPACE;
1377 if (typefaceIndex != -1 && !fontFamilyExplicit) {
1380 setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex);
1382 if (shadowcolor != 0) {
1383 setShadowLayer(r, dx, dy, shadowcolor);
1386 if (maxlength >= 0) {
1387 setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });
1389 setFilters(NO_FILTERS);
1392 setText(text, bufferType);
1393 if (hint != null) setHint(hint);
1396 * Views are not normally focusable unless specified to be.
1397 * However, TextViews that have input or movement methods *are*
1398 * focusable by default.
1400 a = context.obtainStyledAttributes(
1401 attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
1403 boolean focusable = mMovement != null || getKeyListener() != null;
1404 boolean clickable = focusable || isClickable();
1405 boolean longClickable = focusable || isLongClickable();
1407 n = a.getIndexCount();
1408 for (int i = 0; i < n; i++) {
1409 int attr = a.getIndex(i);
1412 case com.android.internal.R.styleable.View_focusable:
1413 focusable = a.getBoolean(attr, focusable);
1416 case com.android.internal.R.styleable.View_clickable:
1417 clickable = a.getBoolean(attr, clickable);
1420 case com.android.internal.R.styleable.View_longClickable:
1421 longClickable = a.getBoolean(attr, longClickable);
1427 setFocusable(focusable);
1428 setClickable(clickable);
1429 setLongClickable(longClickable);
1431 if (mEditor != null) mEditor.prepareCursorControllers();
1433 // If not explicitly specified this view is important for accessibility.
1434 if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
1435 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
1439 private int[] parseDimensionArray(TypedArray dimens) {
1440 if (dimens == null) {
1443 int[] result = new int[dimens.length()];
1444 for (int i = 0; i < result.length; i++) {
1445 result[i] = dimens.getDimensionPixelSize(i, 0);
1454 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1455 if (requestCode == PROCESS_TEXT_REQUEST_CODE) {
1456 if (resultCode == Activity.RESULT_OK && data != null) {
1457 CharSequence result = data.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
1458 if (result != null) {
1459 if (isTextEditable()) {
1460 replaceSelectionWithText(result);
1462 if (result.length() > 0) {
1463 Toast.makeText(getContext(), String.valueOf(result), Toast.LENGTH_LONG)
1469 if (mEditor.hasSelectionController()) {
1470 mEditor.startSelectionActionMode();
1475 private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
1477 if (familyName != null) {
1478 tf = Typeface.create(familyName, styleIndex);
1484 switch (typefaceIndex) {
1486 tf = Typeface.SANS_SERIF;
1490 tf = Typeface.SERIF;
1494 tf = Typeface.MONOSPACE;
1498 setTypeface(tf, styleIndex);
1501 private void setRelativeDrawablesIfNeeded(Drawable start, Drawable end) {
1502 boolean hasRelativeDrawables = (start != null) || (end != null);
1503 if (hasRelativeDrawables) {
1504 Drawables dr = mDrawables;
1506 mDrawables = dr = new Drawables(getContext());
1508 mDrawables.mOverride = true;
1509 final Rect compoundRect = dr.mCompoundRect;
1510 int[] state = getDrawableState();
1511 if (start != null) {
1512 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight());
1513 start.setState(state);
1514 start.copyBounds(compoundRect);
1515 start.setCallback(this);
1517 dr.mDrawableStart = start;
1518 dr.mDrawableSizeStart = compoundRect.width();
1519 dr.mDrawableHeightStart = compoundRect.height();
1521 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0;
1524 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight());
1525 end.setState(state);
1526 end.copyBounds(compoundRect);
1527 end.setCallback(this);
1529 dr.mDrawableEnd = end;
1530 dr.mDrawableSizeEnd = compoundRect.width();
1531 dr.mDrawableHeightEnd = compoundRect.height();
1533 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0;
1535 resetResolvedDrawables();
1537 applyCompoundDrawableTint();
1542 public void setEnabled(boolean enabled) {
1543 if (enabled == isEnabled()) {
1548 // Hide the soft input if the currently active TextView is disabled
1549 InputMethodManager imm = InputMethodManager.peekInstance();
1550 if (imm != null && imm.isActive(this)) {
1551 imm.hideSoftInputFromWindow(getWindowToken(), 0);
1555 super.setEnabled(enabled);
1558 // Make sure IME is updated with current editor info.
1559 InputMethodManager imm = InputMethodManager.peekInstance();
1560 if (imm != null) imm.restartInput(this);
1563 // Will change text color
1564 if (mEditor != null) {
1565 mEditor.invalidateTextDisplayList();
1566 mEditor.prepareCursorControllers();
1568 // start or stop the cursor blinking as appropriate
1569 mEditor.makeBlink();
1574 * Sets the typeface and style in which the text should be displayed,
1575 * and turns on the fake bold and italic bits in the Paint if the
1576 * Typeface that you provided does not have all the bits in the
1577 * style that you specified.
1579 * @attr ref android.R.styleable#TextView_typeface
1580 * @attr ref android.R.styleable#TextView_textStyle
1582 public void setTypeface(Typeface tf, int style) {
1585 tf = Typeface.defaultFromStyle(style);
1587 tf = Typeface.create(tf, style);
1591 // now compute what (if any) algorithmic styling is needed
1592 int typefaceStyle = tf != null ? tf.getStyle() : 0;
1593 int need = style & ~typefaceStyle;
1594 mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
1595 mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
1597 mTextPaint.setFakeBoldText(false);
1598 mTextPaint.setTextSkewX(0);
1604 * Subclasses override this to specify that they have a KeyListener
1605 * by default even if not specifically called for in the XML options.
1607 protected boolean getDefaultEditable() {
1612 * Subclasses override this to specify a default movement method.
1614 protected MovementMethod getDefaultMovementMethod() {
1619 * Return the text the TextView is displaying. If setText() was called with
1620 * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast
1621 * the return value from this method to Spannable or Editable, respectively.
1623 * Note: The content of the return value should not be modified. If you want
1624 * a modifiable one, you should make your own copy first.
1626 * @attr ref android.R.styleable#TextView_text
1628 @ViewDebug.CapturedViewProperty
1629 public CharSequence getText() {
1634 * Returns the length, in characters, of the text managed by this TextView
1636 public int length() {
1637 return mText.length();
1641 * Return the text the TextView is displaying as an Editable object. If
1642 * the text is not editable, null is returned.
1646 public Editable getEditableText() {
1647 return (mText instanceof Editable) ? (Editable)mText : null;
1651 * @return the height of one standard line in pixels. Note that markup
1652 * within the text can cause individual lines to be taller or shorter
1653 * than this height, and the layout may contain additional first-
1654 * or last-line padding.
1656 public int getLineHeight() {
1657 return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd);
1661 * @return the Layout that is currently being used to display the text.
1662 * This can be null if the text or width has recently changes.
1664 public final Layout getLayout() {
1669 * @return the Layout that is currently being used to display the hint text.
1672 final Layout getHintLayout() {
1677 * Retrieve the {@link android.content.UndoManager} that is currently associated
1678 * with this TextView. By default there is no associated UndoManager, so null
1679 * is returned. One can be associated with the TextView through
1680 * {@link #setUndoManager(android.content.UndoManager, String)}
1684 public final UndoManager getUndoManager() {
1685 // TODO: Consider supporting a global undo manager.
1686 throw new UnsupportedOperationException("not implemented");
1690 * Associate an {@link android.content.UndoManager} with this TextView. Once
1691 * done, all edit operations on the TextView will result in appropriate
1692 * {@link android.content.UndoOperation} objects pushed on the given UndoManager's
1695 * @param undoManager The {@link android.content.UndoManager} to associate with
1696 * this TextView, or null to clear any existing association.
1697 * @param tag String tag identifying this particular TextView owner in the
1698 * UndoManager. This is used to keep the correct association with the
1699 * {@link android.content.UndoOwner} of any operations inside of the UndoManager.
1703 public final void setUndoManager(UndoManager undoManager, String tag) {
1704 // TODO: Consider supporting a global undo manager. An implementation will need to:
1705 // * createEditorIfNeeded()
1706 // * Promote to BufferType.EDITABLE if needed.
1707 // * Update the UndoManager and UndoOwner.
1708 // Likewise it will need to be able to restore the default UndoManager.
1709 throw new UnsupportedOperationException("not implemented");
1713 * @return the current key listener for this TextView.
1714 * This will frequently be null for non-EditText TextViews.
1716 * @attr ref android.R.styleable#TextView_numeric
1717 * @attr ref android.R.styleable#TextView_digits
1718 * @attr ref android.R.styleable#TextView_phoneNumber
1719 * @attr ref android.R.styleable#TextView_inputMethod
1720 * @attr ref android.R.styleable#TextView_capitalize
1721 * @attr ref android.R.styleable#TextView_autoText
1723 public final KeyListener getKeyListener() {
1724 return mEditor == null ? null : mEditor.mKeyListener;
1728 * Sets the key listener to be used with this TextView. This can be null
1729 * to disallow user input. Note that this method has significant and
1730 * subtle interactions with soft keyboards and other input method:
1731 * see {@link KeyListener#getInputType() KeyListener.getContentType()}
1732 * for important details. Calling this method will replace the current
1733 * content type of the text view with the content type returned by the
1736 * Be warned that if you want a TextView with a key listener or movement
1737 * method not to be focusable, or if you want a TextView without a
1738 * key listener or movement method to be focusable, you must call
1739 * {@link #setFocusable} again after calling this to get the focusability
1740 * back the way you want it.
1742 * @attr ref android.R.styleable#TextView_numeric
1743 * @attr ref android.R.styleable#TextView_digits
1744 * @attr ref android.R.styleable#TextView_phoneNumber
1745 * @attr ref android.R.styleable#TextView_inputMethod
1746 * @attr ref android.R.styleable#TextView_capitalize
1747 * @attr ref android.R.styleable#TextView_autoText
1749 public void setKeyListener(KeyListener input) {
1750 setKeyListenerOnly(input);
1751 fixFocusableAndClickableSettings();
1753 if (input != null) {
1754 createEditorIfNeeded();
1756 mEditor.mInputType = mEditor.mKeyListener.getInputType();
1757 } catch (IncompatibleClassChangeError e) {
1758 mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT;
1760 // Change inputType, without affecting transformation.
1761 // No need to applySingleLine since mSingleLine is unchanged.
1762 setInputTypeSingleLine(mSingleLine);
1764 if (mEditor != null) mEditor.mInputType = EditorInfo.TYPE_NULL;
1767 InputMethodManager imm = InputMethodManager.peekInstance();
1768 if (imm != null) imm.restartInput(this);
1771 private void setKeyListenerOnly(KeyListener input) {
1772 if (mEditor == null && input == null) return; // null is the default value
1774 createEditorIfNeeded();
1775 if (mEditor.mKeyListener != input) {
1776 mEditor.mKeyListener = input;
1777 if (input != null && !(mText instanceof Editable)) {
1781 setFilters((Editable) mText, mFilters);
1786 * @return the movement method being used for this TextView.
1787 * This will frequently be null for non-EditText TextViews.
1789 public final MovementMethod getMovementMethod() {
1794 * Sets the movement method (arrow key handler) to be used for
1795 * this TextView. This can be null to disallow using the arrow keys
1796 * to move the cursor or scroll the view.
1798 * Be warned that if you want a TextView with a key listener or movement
1799 * method not to be focusable, or if you want a TextView without a
1800 * key listener or movement method to be focusable, you must call
1801 * {@link #setFocusable} again after calling this to get the focusability
1802 * back the way you want it.
1804 public final void setMovementMethod(MovementMethod movement) {
1805 if (mMovement != movement) {
1806 mMovement = movement;
1808 if (movement != null && !(mText instanceof Spannable)) {
1812 fixFocusableAndClickableSettings();
1814 // SelectionModifierCursorController depends on textCanBeSelected, which depends on
1816 if (mEditor != null) mEditor.prepareCursorControllers();
1820 private void fixFocusableAndClickableSettings() {
1821 if (mMovement != null || (mEditor != null && mEditor.mKeyListener != null)) {
1824 setLongClickable(true);
1826 setFocusable(false);
1827 setClickable(false);
1828 setLongClickable(false);
1833 * @return the current transformation method for this TextView.
1834 * This will frequently be null except for single-line and password
1837 * @attr ref android.R.styleable#TextView_password
1838 * @attr ref android.R.styleable#TextView_singleLine
1840 public final TransformationMethod getTransformationMethod() {
1841 return mTransformation;
1845 * Sets the transformation that is applied to the text that this
1846 * TextView is displaying.
1848 * @attr ref android.R.styleable#TextView_password
1849 * @attr ref android.R.styleable#TextView_singleLine
1851 public final void setTransformationMethod(TransformationMethod method) {
1852 if (method == mTransformation) {
1853 // Avoid the setText() below if the transformation is
1857 if (mTransformation != null) {
1858 if (mText instanceof Spannable) {
1859 ((Spannable) mText).removeSpan(mTransformation);
1863 mTransformation = method;
1865 if (method instanceof TransformationMethod2) {
1866 TransformationMethod2 method2 = (TransformationMethod2) method;
1867 mAllowTransformationLengthChange = !isTextSelectable() && !(mText instanceof Editable);
1868 method2.setLengthChangesAllowed(mAllowTransformationLengthChange);
1870 mAllowTransformationLengthChange = false;
1875 if (hasPasswordTransformationMethod()) {
1876 notifyViewAccessibilityStateChangedIfNeeded(
1877 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
1882 * Returns the top padding of the view, plus space for the top
1885 public int getCompoundPaddingTop() {
1886 final Drawables dr = mDrawables;
1887 if (dr == null || dr.mShowing[Drawables.TOP] == null) {
1890 return mPaddingTop + dr.mDrawablePadding + dr.mDrawableSizeTop;
1895 * Returns the bottom padding of the view, plus space for the bottom
1898 public int getCompoundPaddingBottom() {
1899 final Drawables dr = mDrawables;
1900 if (dr == null || dr.mShowing[Drawables.BOTTOM] == null) {
1901 return mPaddingBottom;
1903 return mPaddingBottom + dr.mDrawablePadding + dr.mDrawableSizeBottom;
1908 * Returns the left padding of the view, plus space for the left
1911 public int getCompoundPaddingLeft() {
1912 final Drawables dr = mDrawables;
1913 if (dr == null || dr.mShowing[Drawables.LEFT] == null) {
1914 return mPaddingLeft;
1916 return mPaddingLeft + dr.mDrawablePadding + dr.mDrawableSizeLeft;
1921 * Returns the right padding of the view, plus space for the right
1924 public int getCompoundPaddingRight() {
1925 final Drawables dr = mDrawables;
1926 if (dr == null || dr.mShowing[Drawables.RIGHT] == null) {
1927 return mPaddingRight;
1929 return mPaddingRight + dr.mDrawablePadding + dr.mDrawableSizeRight;
1934 * Returns the start padding of the view, plus space for the start
1937 public int getCompoundPaddingStart() {
1939 switch(getLayoutDirection()) {
1941 case LAYOUT_DIRECTION_LTR:
1942 return getCompoundPaddingLeft();
1943 case LAYOUT_DIRECTION_RTL:
1944 return getCompoundPaddingRight();
1949 * Returns the end padding of the view, plus space for the end
1952 public int getCompoundPaddingEnd() {
1954 switch(getLayoutDirection()) {
1956 case LAYOUT_DIRECTION_LTR:
1957 return getCompoundPaddingRight();
1958 case LAYOUT_DIRECTION_RTL:
1959 return getCompoundPaddingLeft();
1964 * Returns the extended top padding of the view, including both the
1965 * top Drawable if any and any extra space to keep more than maxLines
1966 * of text from showing. It is only valid to call this after measuring.
1968 public int getExtendedPaddingTop() {
1969 if (mMaxMode != LINES) {
1970 return getCompoundPaddingTop();
1973 if (mLayout == null) {
1977 if (mLayout.getLineCount() <= mMaximum) {
1978 return getCompoundPaddingTop();
1981 int top = getCompoundPaddingTop();
1982 int bottom = getCompoundPaddingBottom();
1983 int viewht = getHeight() - top - bottom;
1984 int layoutht = mLayout.getLineTop(mMaximum);
1986 if (layoutht >= viewht) {
1990 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1991 if (gravity == Gravity.TOP) {
1993 } else if (gravity == Gravity.BOTTOM) {
1994 return top + viewht - layoutht;
1995 } else { // (gravity == Gravity.CENTER_VERTICAL)
1996 return top + (viewht - layoutht) / 2;
2001 * Returns the extended bottom padding of the view, including both the
2002 * bottom Drawable if any and any extra space to keep more than maxLines
2003 * of text from showing. It is only valid to call this after measuring.
2005 public int getExtendedPaddingBottom() {
2006 if (mMaxMode != LINES) {
2007 return getCompoundPaddingBottom();
2010 if (mLayout == null) {
2014 if (mLayout.getLineCount() <= mMaximum) {
2015 return getCompoundPaddingBottom();
2018 int top = getCompoundPaddingTop();
2019 int bottom = getCompoundPaddingBottom();
2020 int viewht = getHeight() - top - bottom;
2021 int layoutht = mLayout.getLineTop(mMaximum);
2023 if (layoutht >= viewht) {
2027 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
2028 if (gravity == Gravity.TOP) {
2029 return bottom + viewht - layoutht;
2030 } else if (gravity == Gravity.BOTTOM) {
2032 } else { // (gravity == Gravity.CENTER_VERTICAL)
2033 return bottom + (viewht - layoutht) / 2;
2038 * Returns the total left padding of the view, including the left
2041 public int getTotalPaddingLeft() {
2042 return getCompoundPaddingLeft();
2046 * Returns the total right padding of the view, including the right
2049 public int getTotalPaddingRight() {
2050 return getCompoundPaddingRight();
2054 * Returns the total start padding of the view, including the start
2057 public int getTotalPaddingStart() {
2058 return getCompoundPaddingStart();
2062 * Returns the total end padding of the view, including the end
2065 public int getTotalPaddingEnd() {
2066 return getCompoundPaddingEnd();
2070 * Returns the total top padding of the view, including the top
2071 * Drawable if any, the extra space to keep more than maxLines
2072 * from showing, and the vertical offset for gravity, if any.
2074 public int getTotalPaddingTop() {
2075 return getExtendedPaddingTop() + getVerticalOffset(true);
2079 * Returns the total bottom padding of the view, including the bottom
2080 * Drawable if any, the extra space to keep more than maxLines
2081 * from showing, and the vertical offset for gravity, if any.
2083 public int getTotalPaddingBottom() {
2084 return getExtendedPaddingBottom() + getBottomVerticalOffset(true);
2088 * Sets the Drawables (if any) to appear to the left of, above, to the
2089 * right of, and below the text. Use {@code null} if you do not want a
2090 * Drawable there. The Drawables must already have had
2091 * {@link Drawable#setBounds} called.
2093 * Calling this method will overwrite any Drawables previously set using
2094 * {@link #setCompoundDrawablesRelative} or related methods.
2096 * @attr ref android.R.styleable#TextView_drawableLeft
2097 * @attr ref android.R.styleable#TextView_drawableTop
2098 * @attr ref android.R.styleable#TextView_drawableRight
2099 * @attr ref android.R.styleable#TextView_drawableBottom
2101 public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top,
2102 @Nullable Drawable right, @Nullable Drawable bottom) {
2103 Drawables dr = mDrawables;
2105 // We're switching to absolute, discard relative.
2107 if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null);
2108 dr.mDrawableStart = null;
2109 if (dr.mDrawableEnd != null) dr.mDrawableEnd.setCallback(null);
2110 dr.mDrawableEnd = null;
2111 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0;
2112 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0;
2115 final boolean drawables = left != null || top != null || right != null || bottom != null;
2117 // Clearing drawables... can we free the data structure?
2119 if (dr.mDrawablePadding == 0) {
2122 // We need to retain the last set padding, so just clear
2123 // out all of the fields in the existing structure.
2124 for (int i = dr.mShowing.length - 1; i >= 0; i--) {
2125 if (dr.mShowing[i] != null) {
2126 dr.mShowing[i].setCallback(null);
2128 dr.mShowing[i] = null;
2130 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
2131 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
2132 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
2133 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
2138 mDrawables = dr = new Drawables(getContext());
2141 mDrawables.mOverride = false;
2143 if (dr.mShowing[Drawables.LEFT] != left && dr.mShowing[Drawables.LEFT] != null) {
2144 dr.mShowing[Drawables.LEFT].setCallback(null);
2146 dr.mShowing[Drawables.LEFT] = left;
2148 if (dr.mShowing[Drawables.TOP] != top && dr.mShowing[Drawables.TOP] != null) {
2149 dr.mShowing[Drawables.TOP].setCallback(null);
2151 dr.mShowing[Drawables.TOP] = top;
2153 if (dr.mShowing[Drawables.RIGHT] != right && dr.mShowing[Drawables.RIGHT] != null) {
2154 dr.mShowing[Drawables.RIGHT].setCallback(null);
2156 dr.mShowing[Drawables.RIGHT] = right;
2158 if (dr.mShowing[Drawables.BOTTOM] != bottom && dr.mShowing[Drawables.BOTTOM] != null) {
2159 dr.mShowing[Drawables.BOTTOM].setCallback(null);
2161 dr.mShowing[Drawables.BOTTOM] = bottom;
2163 final Rect compoundRect = dr.mCompoundRect;
2166 state = getDrawableState();
2169 left.setState(state);
2170 left.copyBounds(compoundRect);
2171 left.setCallback(this);
2172 dr.mDrawableSizeLeft = compoundRect.width();
2173 dr.mDrawableHeightLeft = compoundRect.height();
2175 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
2178 if (right != null) {
2179 right.setState(state);
2180 right.copyBounds(compoundRect);
2181 right.setCallback(this);
2182 dr.mDrawableSizeRight = compoundRect.width();
2183 dr.mDrawableHeightRight = compoundRect.height();
2185 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
2189 top.setState(state);
2190 top.copyBounds(compoundRect);
2191 top.setCallback(this);
2192 dr.mDrawableSizeTop = compoundRect.height();
2193 dr.mDrawableWidthTop = compoundRect.width();
2195 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
2198 if (bottom != null) {
2199 bottom.setState(state);
2200 bottom.copyBounds(compoundRect);
2201 bottom.setCallback(this);
2202 dr.mDrawableSizeBottom = compoundRect.height();
2203 dr.mDrawableWidthBottom = compoundRect.width();
2205 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
2209 // Save initial left/right drawables
2211 dr.mDrawableLeftInitial = left;
2212 dr.mDrawableRightInitial = right;
2215 resetResolvedDrawables();
2217 applyCompoundDrawableTint();
2223 * Sets the Drawables (if any) to appear to the left of, above, to the
2224 * right of, and below the text. Use 0 if you do not want a Drawable there.
2225 * The Drawables' bounds will be set to their intrinsic bounds.
2227 * Calling this method will overwrite any Drawables previously set using
2228 * {@link #setCompoundDrawablesRelative} or related methods.
2230 * @param left Resource identifier of the left Drawable.
2231 * @param top Resource identifier of the top Drawable.
2232 * @param right Resource identifier of the right Drawable.
2233 * @param bottom Resource identifier of the bottom Drawable.
2235 * @attr ref android.R.styleable#TextView_drawableLeft
2236 * @attr ref android.R.styleable#TextView_drawableTop
2237 * @attr ref android.R.styleable#TextView_drawableRight
2238 * @attr ref android.R.styleable#TextView_drawableBottom
2240 @android.view.RemotableViewMethod
2241 public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left,
2242 @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
2243 final Context context = getContext();
2244 setCompoundDrawablesWithIntrinsicBounds(left != 0 ? context.getDrawable(left) : null,
2245 top != 0 ? context.getDrawable(top) : null,
2246 right != 0 ? context.getDrawable(right) : null,
2247 bottom != 0 ? context.getDrawable(bottom) : null);
2251 * Sets the Drawables (if any) to appear to the left of, above, to the
2252 * right of, and below the text. Use {@code null} if you do not want a
2253 * Drawable there. The Drawables' bounds will be set to their intrinsic
2256 * Calling this method will overwrite any Drawables previously set using
2257 * {@link #setCompoundDrawablesRelative} or related methods.
2259 * @attr ref android.R.styleable#TextView_drawableLeft
2260 * @attr ref android.R.styleable#TextView_drawableTop
2261 * @attr ref android.R.styleable#TextView_drawableRight
2262 * @attr ref android.R.styleable#TextView_drawableBottom
2264 public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left,
2265 @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
2268 left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
2270 if (right != null) {
2271 right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
2274 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
2276 if (bottom != null) {
2277 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
2279 setCompoundDrawables(left, top, right, bottom);
2283 * Sets the Drawables (if any) to appear to the start of, above, to the end
2284 * of, and below the text. Use {@code null} if you do not want a Drawable
2285 * there. The Drawables must already have had {@link Drawable#setBounds}
2288 * Calling this method will overwrite any Drawables previously set using
2289 * {@link #setCompoundDrawables} or related methods.
2291 * @attr ref android.R.styleable#TextView_drawableStart
2292 * @attr ref android.R.styleable#TextView_drawableTop
2293 * @attr ref android.R.styleable#TextView_drawableEnd
2294 * @attr ref android.R.styleable#TextView_drawableBottom
2296 public void setCompoundDrawablesRelative(@Nullable Drawable start, @Nullable Drawable top,
2297 @Nullable Drawable end, @Nullable Drawable bottom) {
2298 Drawables dr = mDrawables;
2300 // We're switching to relative, discard absolute.
2302 if (dr.mShowing[Drawables.LEFT] != null) {
2303 dr.mShowing[Drawables.LEFT].setCallback(null);
2305 dr.mShowing[Drawables.LEFT] = dr.mDrawableLeftInitial = null;
2306 if (dr.mShowing[Drawables.RIGHT] != null) {
2307 dr.mShowing[Drawables.RIGHT].setCallback(null);
2309 dr.mShowing[Drawables.RIGHT] = dr.mDrawableRightInitial = null;
2310 dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
2311 dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
2314 final boolean drawables = start != null || top != null
2315 || end != null || bottom != null;
2318 // Clearing drawables... can we free the data structure?
2320 if (dr.mDrawablePadding == 0) {
2323 // We need to retain the last set padding, so just clear
2324 // out all of the fields in the existing structure.
2325 if (dr.mDrawableStart != null) dr.mDrawableStart.setCallback(null);
2326 dr.mDrawableStart = null;
2327 if (dr.mShowing[Drawables.TOP] != null) {
2328 dr.mShowing[Drawables.TOP].setCallback(null);
2330 dr.mShowing[Drawables.TOP] = null;
2331 if (dr.mDrawableEnd != null) {
2332 dr.mDrawableEnd.setCallback(null);
2334 dr.mDrawableEnd = null;
2335 if (dr.mShowing[Drawables.BOTTOM] != null) {
2336 dr.mShowing[Drawables.BOTTOM].setCallback(null);
2338 dr.mShowing[Drawables.BOTTOM] = null;
2339 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0;
2340 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0;
2341 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
2342 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
2347 mDrawables = dr = new Drawables(getContext());
2350 mDrawables.mOverride = true;
2352 if (dr.mDrawableStart != start && dr.mDrawableStart != null) {
2353 dr.mDrawableStart.setCallback(null);
2355 dr.mDrawableStart = start;
2357 if (dr.mShowing[Drawables.TOP] != top && dr.mShowing[Drawables.TOP] != null) {
2358 dr.mShowing[Drawables.TOP].setCallback(null);
2360 dr.mShowing[Drawables.TOP] = top;
2362 if (dr.mDrawableEnd != end && dr.mDrawableEnd != null) {
2363 dr.mDrawableEnd.setCallback(null);
2365 dr.mDrawableEnd = end;
2367 if (dr.mShowing[Drawables.BOTTOM] != bottom && dr.mShowing[Drawables.BOTTOM] != null) {
2368 dr.mShowing[Drawables.BOTTOM].setCallback(null);
2370 dr.mShowing[Drawables.BOTTOM] = bottom;
2372 final Rect compoundRect = dr.mCompoundRect;
2375 state = getDrawableState();
2377 if (start != null) {
2378 start.setState(state);
2379 start.copyBounds(compoundRect);
2380 start.setCallback(this);
2381 dr.mDrawableSizeStart = compoundRect.width();
2382 dr.mDrawableHeightStart = compoundRect.height();
2384 dr.mDrawableSizeStart = dr.mDrawableHeightStart = 0;
2388 end.setState(state);
2389 end.copyBounds(compoundRect);
2390 end.setCallback(this);
2391 dr.mDrawableSizeEnd = compoundRect.width();
2392 dr.mDrawableHeightEnd = compoundRect.height();
2394 dr.mDrawableSizeEnd = dr.mDrawableHeightEnd = 0;
2398 top.setState(state);
2399 top.copyBounds(compoundRect);
2400 top.setCallback(this);
2401 dr.mDrawableSizeTop = compoundRect.height();
2402 dr.mDrawableWidthTop = compoundRect.width();
2404 dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
2407 if (bottom != null) {
2408 bottom.setState(state);
2409 bottom.copyBounds(compoundRect);
2410 bottom.setCallback(this);
2411 dr.mDrawableSizeBottom = compoundRect.height();
2412 dr.mDrawableWidthBottom = compoundRect.width();
2414 dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
2418 resetResolvedDrawables();
2425 * Sets the Drawables (if any) to appear to the start of, above, to the end
2426 * of, and below the text. Use 0 if you do not want a Drawable there. The
2427 * Drawables' bounds will be set to their intrinsic bounds.
2429 * Calling this method will overwrite any Drawables previously set using
2430 * {@link #setCompoundDrawables} or related methods.
2432 * @param start Resource identifier of the start Drawable.
2433 * @param top Resource identifier of the top Drawable.
2434 * @param end Resource identifier of the end Drawable.
2435 * @param bottom Resource identifier of the bottom Drawable.
2437 * @attr ref android.R.styleable#TextView_drawableStart
2438 * @attr ref android.R.styleable#TextView_drawableTop
2439 * @attr ref android.R.styleable#TextView_drawableEnd
2440 * @attr ref android.R.styleable#TextView_drawableBottom
2442 @android.view.RemotableViewMethod
2443 public void setCompoundDrawablesRelativeWithIntrinsicBounds(@DrawableRes int start,
2444 @DrawableRes int top, @DrawableRes int end, @DrawableRes int bottom) {
2445 final Context context = getContext();
2446 setCompoundDrawablesRelativeWithIntrinsicBounds(
2447 start != 0 ? context.getDrawable(start) : null,
2448 top != 0 ? context.getDrawable(top) : null,
2449 end != 0 ? context.getDrawable(end) : null,
2450 bottom != 0 ? context.getDrawable(bottom) : null);
2454 * Sets the Drawables (if any) to appear to the start of, above, to the end
2455 * of, and below the text. Use {@code null} if you do not want a Drawable
2456 * there. The Drawables' bounds will be set to their intrinsic bounds.
2458 * Calling this method will overwrite any Drawables previously set using
2459 * {@link #setCompoundDrawables} or related methods.
2461 * @attr ref android.R.styleable#TextView_drawableStart
2462 * @attr ref android.R.styleable#TextView_drawableTop
2463 * @attr ref android.R.styleable#TextView_drawableEnd
2464 * @attr ref android.R.styleable#TextView_drawableBottom
2466 public void setCompoundDrawablesRelativeWithIntrinsicBounds(@Nullable Drawable start,
2467 @Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom) {
2469 if (start != null) {
2470 start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight());
2473 end.setBounds(0, 0, end.getIntrinsicWidth(), end.getIntrinsicHeight());
2476 top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
2478 if (bottom != null) {
2479 bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
2481 setCompoundDrawablesRelative(start, top, end, bottom);
2485 * Returns drawables for the left, top, right, and bottom borders.
2487 * @attr ref android.R.styleable#TextView_drawableLeft
2488 * @attr ref android.R.styleable#TextView_drawableTop
2489 * @attr ref android.R.styleable#TextView_drawableRight
2490 * @attr ref android.R.styleable#TextView_drawableBottom
2493 public Drawable[] getCompoundDrawables() {
2494 final Drawables dr = mDrawables;
2496 return dr.mShowing.clone();
2498 return new Drawable[] { null, null, null, null };
2503 * Returns drawables for the start, top, end, and bottom borders.
2505 * @attr ref android.R.styleable#TextView_drawableStart
2506 * @attr ref android.R.styleable#TextView_drawableTop
2507 * @attr ref android.R.styleable#TextView_drawableEnd
2508 * @attr ref android.R.styleable#TextView_drawableBottom
2511 public Drawable[] getCompoundDrawablesRelative() {
2512 final Drawables dr = mDrawables;
2514 return new Drawable[] {
2515 dr.mDrawableStart, dr.mShowing[Drawables.TOP],
2516 dr.mDrawableEnd, dr.mShowing[Drawables.BOTTOM]
2519 return new Drawable[] { null, null, null, null };
2524 * Sets the size of the padding between the compound drawables and
2527 * @attr ref android.R.styleable#TextView_drawablePadding
2529 @android.view.RemotableViewMethod
2530 public void setCompoundDrawablePadding(int pad) {
2531 Drawables dr = mDrawables;
2534 dr.mDrawablePadding = pad;
2538 mDrawables = dr = new Drawables(getContext());
2540 dr.mDrawablePadding = pad;
2548 * Returns the padding between the compound drawables and the text.
2550 * @attr ref android.R.styleable#TextView_drawablePadding
2552 public int getCompoundDrawablePadding() {
2553 final Drawables dr = mDrawables;
2554 return dr != null ? dr.mDrawablePadding : 0;
2558 * Applies a tint to the compound drawables. Does not modify the
2559 * current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
2561 * Subsequent calls to
2562 * {@link #setCompoundDrawables(Drawable, Drawable, Drawable, Drawable)}
2563 * and related methods will automatically mutate the drawables and apply
2564 * the specified tint and tint mode using
2565 * {@link Drawable#setTintList(ColorStateList)}.
2567 * @param tint the tint to apply, may be {@code null} to clear tint
2569 * @attr ref android.R.styleable#TextView_drawableTint
2570 * @see #getCompoundDrawableTintList()
2571 * @see Drawable#setTintList(ColorStateList)
2573 public void setCompoundDrawableTintList(@Nullable ColorStateList tint) {
2574 if (mDrawables == null) {
2575 mDrawables = new Drawables(getContext());
2577 mDrawables.mTintList = tint;
2578 mDrawables.mHasTint = true;
2580 applyCompoundDrawableTint();
2584 * @return the tint applied to the compound drawables
2585 * @attr ref android.R.styleable#TextView_drawableTint
2586 * @see #setCompoundDrawableTintList(ColorStateList)
2588 public ColorStateList getCompoundDrawableTintList() {
2589 return mDrawables != null ? mDrawables.mTintList : null;
2593 * Specifies the blending mode used to apply the tint specified by
2594 * {@link #setCompoundDrawableTintList(ColorStateList)} to the compound
2595 * drawables. The default mode is {@link PorterDuff.Mode#SRC_IN}.
2597 * @param tintMode the blending mode used to apply the tint, may be
2598 * {@code null} to clear tint
2599 * @attr ref android.R.styleable#TextView_drawableTintMode
2600 * @see #setCompoundDrawableTintList(ColorStateList)
2601 * @see Drawable#setTintMode(PorterDuff.Mode)
2603 public void setCompoundDrawableTintMode(@Nullable PorterDuff.Mode tintMode) {
2604 if (mDrawables == null) {
2605 mDrawables = new Drawables(getContext());
2607 mDrawables.mTintMode = tintMode;
2608 mDrawables.mHasTintMode = true;
2610 applyCompoundDrawableTint();
2614 * Returns the blending mode used to apply the tint to the compound
2615 * drawables, if specified.
2617 * @return the blending mode used to apply the tint to the compound
2619 * @attr ref android.R.styleable#TextView_drawableTintMode
2620 * @see #setCompoundDrawableTintMode(PorterDuff.Mode)
2622 public PorterDuff.Mode getCompoundDrawableTintMode() {
2623 return mDrawables != null ? mDrawables.mTintMode : null;
2626 private void applyCompoundDrawableTint() {
2627 if (mDrawables == null) {
2631 if (mDrawables.mHasTint || mDrawables.mHasTintMode) {
2632 final ColorStateList tintList = mDrawables.mTintList;
2633 final PorterDuff.Mode tintMode = mDrawables.mTintMode;
2634 final boolean hasTint = mDrawables.mHasTint;
2635 final boolean hasTintMode = mDrawables.mHasTintMode;
2636 final int[] state = getDrawableState();
2638 for (Drawable dr : mDrawables.mShowing) {
2643 if (dr == mDrawables.mDrawableError) {
2644 // From a developer's perspective, the error drawable isn't
2645 // a compound drawable. Don't apply the generic compound
2646 // drawable tint to it.
2653 dr.setTintList(tintList);
2657 dr.setTintMode(tintMode);
2660 // The drawable (or one of its children) may not have been
2661 // stateful before applying the tint, so let's try again.
2662 if (dr.isStateful()) {
2670 public void setPadding(int left, int top, int right, int bottom) {
2671 if (left != mPaddingLeft ||
2672 right != mPaddingRight ||
2673 top != mPaddingTop ||
2674 bottom != mPaddingBottom) {
2678 // the super call will requestLayout()
2679 super.setPadding(left, top, right, bottom);
2684 public void setPaddingRelative(int start, int top, int end, int bottom) {
2685 if (start != getPaddingStart() ||
2686 end != getPaddingEnd() ||
2687 top != mPaddingTop ||
2688 bottom != mPaddingBottom) {
2692 // the super call will requestLayout()
2693 super.setPaddingRelative(start, top, end, bottom);
2698 * Gets the autolink mask of the text. See {@link
2699 * android.text.util.Linkify#ALL Linkify.ALL} and peers for
2702 * @attr ref android.R.styleable#TextView_autoLink
2704 public final int getAutoLinkMask() {
2705 return mAutoLinkMask;
2709 * Sets the text appearance from the specified style resource.
2711 * Use a framework-defined {@code TextAppearance} style like
2712 * {@link android.R.style#TextAppearance_Material_Body1 @android:style/TextAppearance.Material.Body1}
2713 * or see {@link android.R.styleable#TextAppearance TextAppearance} for the
2714 * set of attributes that can be used in a custom style.
2716 * @param resId the resource identifier of the style to apply
2717 * @attr ref android.R.styleable#TextView_textAppearance
2719 @SuppressWarnings("deprecation")
2720 public void setTextAppearance(@StyleRes int resId) {
2721 setTextAppearance(mContext, resId);
2725 * Sets the text color, size, style, hint color, and highlight color
2726 * from the specified TextAppearance resource.
2728 * @deprecated Use {@link #setTextAppearance(int)} instead.
2731 public void setTextAppearance(Context context, @StyleRes int resId) {
2732 final TypedArray ta = context.obtainStyledAttributes(resId, R.styleable.TextAppearance);
2734 final int textColorHighlight = ta.getColor(
2735 R.styleable.TextAppearance_textColorHighlight, 0);
2736 if (textColorHighlight != 0) {
2737 setHighlightColor(textColorHighlight);
2740 final ColorStateList textColor = ta.getColorStateList(R.styleable.TextAppearance_textColor);
2741 if (textColor != null) {
2742 setTextColor(textColor);
2745 final int textSize = ta.getDimensionPixelSize(R.styleable.TextAppearance_textSize, 0);
2746 if (textSize != 0) {
2747 setRawTextSize(textSize);
2750 final ColorStateList textColorHint = ta.getColorStateList(
2751 R.styleable.TextAppearance_textColorHint);
2752 if (textColorHint != null) {
2753 setHintTextColor(textColorHint);
2756 final ColorStateList textColorLink = ta.getColorStateList(
2757 R.styleable.TextAppearance_textColorLink);
2758 if (textColorLink != null) {
2759 setLinkTextColor(textColorLink);
2762 final String fontFamily = ta.getString(R.styleable.TextAppearance_fontFamily);
2763 final int typefaceIndex = ta.getInt(R.styleable.TextAppearance_typeface, -1);
2764 final int styleIndex = ta.getInt(R.styleable.TextAppearance_textStyle, -1);
2765 setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex);
2767 final int shadowColor = ta.getInt(R.styleable.TextAppearance_shadowColor, 0);
2768 if (shadowColor != 0) {
2769 final float dx = ta.getFloat(R.styleable.TextAppearance_shadowDx, 0);
2770 final float dy = ta.getFloat(R.styleable.TextAppearance_shadowDy, 0);
2771 final float r = ta.getFloat(R.styleable.TextAppearance_shadowRadius, 0);
2772 setShadowLayer(r, dx, dy, shadowColor);
2775 if (ta.getBoolean(R.styleable.TextAppearance_textAllCaps, false)) {
2776 setTransformationMethod(new AllCapsTransformationMethod(getContext()));
2779 if (ta.hasValue(R.styleable.TextAppearance_elegantTextHeight)) {
2780 setElegantTextHeight(ta.getBoolean(
2781 R.styleable.TextAppearance_elegantTextHeight, false));
2784 if (ta.hasValue(R.styleable.TextAppearance_letterSpacing)) {
2785 setLetterSpacing(ta.getFloat(
2786 R.styleable.TextAppearance_letterSpacing, 0));
2789 if (ta.hasValue(R.styleable.TextAppearance_fontFeatureSettings)) {
2790 setFontFeatureSettings(ta.getString(
2791 R.styleable.TextAppearance_fontFeatureSettings));
2798 * Get the default {@link Locale} of the text in this TextView.
2799 * @return the default {@link Locale} of the text in this TextView.
2801 public Locale getTextLocale() {
2802 return mTextPaint.getTextLocale();
2806 * Set the default {@link Locale} of the text in this TextView to the given value. This value
2807 * is used to choose appropriate typefaces for ambiguous characters. Typically used for CJK
2808 * locales to disambiguate Hanzi/Kanji/Hanja characters.
2810 * @param locale the {@link Locale} for drawing text, must not be null.
2812 * @see Paint#setTextLocale
2814 public void setTextLocale(Locale locale) {
2815 mLocaleChanged = true;
2816 mTextPaint.setTextLocale(locale);
2820 protected void onConfigurationChanged(Configuration newConfig) {
2821 super.onConfigurationChanged(newConfig);
2822 if (!mLocaleChanged) {
2823 mTextPaint.setTextLocale(Locale.getDefault());
2828 * @return the size (in pixels) of the default text size in this TextView.
2830 @ViewDebug.ExportedProperty(category = "text")
2831 public float getTextSize() {
2832 return mTextPaint.getTextSize();
2836 * @return the size (in scaled pixels) of thee default text size in this TextView.
2839 @ViewDebug.ExportedProperty(category = "text")
2840 public float getScaledTextSize() {
2841 return mTextPaint.getTextSize() / mTextPaint.density;
2845 @ViewDebug.ExportedProperty(category = "text", mapping = {
2846 @ViewDebug.IntToString(from = Typeface.NORMAL, to = "NORMAL"),
2847 @ViewDebug.IntToString(from = Typeface.BOLD, to = "BOLD"),
2848 @ViewDebug.IntToString(from = Typeface.ITALIC, to = "ITALIC"),
2849 @ViewDebug.IntToString(from = Typeface.BOLD_ITALIC, to = "BOLD_ITALIC")
2851 public int getTypefaceStyle() {
2852 Typeface typeface = mTextPaint.getTypeface();
2853 return typeface != null ? typeface.getStyle() : Typeface.NORMAL;
2857 * Set the default text size to the given value, interpreted as "scaled
2858 * pixel" units. This size is adjusted based on the current density and
2859 * user font size preference.
2861 * @param size The scaled pixel size.
2863 * @attr ref android.R.styleable#TextView_textSize
2865 @android.view.RemotableViewMethod
2866 public void setTextSize(float size) {
2867 setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
2871 * Set the default text size to a given unit and value. See {@link
2872 * TypedValue} for the possible dimension units.
2874 * @param unit The desired dimension unit.
2875 * @param size The desired size in the given units.
2877 * @attr ref android.R.styleable#TextView_textSize
2879 public void setTextSize(int unit, float size) {
2880 Context c = getContext();
2884 r = Resources.getSystem();
2886 r = c.getResources();
2888 setRawTextSize(TypedValue.applyDimension(
2889 unit, size, r.getDisplayMetrics()));
2892 private void setRawTextSize(float size) {
2893 if (size != mTextPaint.getTextSize()) {
2894 mTextPaint.setTextSize(size);
2896 if (mLayout != null) {
2905 * @return the extent by which text is currently being stretched
2906 * horizontally. This will usually be 1.
2908 public float getTextScaleX() {
2909 return mTextPaint.getTextScaleX();
2913 * Sets the extent by which text should be stretched horizontally.
2915 * @attr ref android.R.styleable#TextView_textScaleX
2917 @android.view.RemotableViewMethod
2918 public void setTextScaleX(float size) {
2919 if (size != mTextPaint.getTextScaleX()) {
2920 mUserSetTextScaleX = true;
2921 mTextPaint.setTextScaleX(size);
2923 if (mLayout != null) {
2932 * Sets the typeface and style in which the text should be displayed.
2933 * Note that not all Typeface families actually have bold and italic
2934 * variants, so you may need to use
2935 * {@link #setTypeface(Typeface, int)} to get the appearance
2936 * that you actually want.
2938 * @see #getTypeface()
2940 * @attr ref android.R.styleable#TextView_fontFamily
2941 * @attr ref android.R.styleable#TextView_typeface
2942 * @attr ref android.R.styleable#TextView_textStyle
2944 public void setTypeface(Typeface tf) {
2945 if (mTextPaint.getTypeface() != tf) {
2946 mTextPaint.setTypeface(tf);
2948 if (mLayout != null) {
2957 * @return the current typeface and style in which the text is being
2960 * @see #setTypeface(Typeface)
2962 * @attr ref android.R.styleable#TextView_fontFamily
2963 * @attr ref android.R.styleable#TextView_typeface
2964 * @attr ref android.R.styleable#TextView_textStyle
2966 public Typeface getTypeface() {
2967 return mTextPaint.getTypeface();
2971 * Set the TextView's elegant height metrics flag. This setting selects font
2972 * variants that have not been compacted to fit Latin-based vertical
2973 * metrics, and also increases top and bottom bounds to provide more space.
2975 * @param elegant set the paint's elegant metrics flag.
2977 * @attr ref android.R.styleable#TextView_elegantTextHeight
2979 public void setElegantTextHeight(boolean elegant) {
2980 mTextPaint.setElegantTextHeight(elegant);
2984 * @return the extent by which text is currently being letter-spaced.
2985 * This will normally be 0.
2987 * @see #setLetterSpacing(float)
2988 * @see Paint#setLetterSpacing
2990 public float getLetterSpacing() {
2991 return mTextPaint.getLetterSpacing();
2995 * Sets text letter-spacing. The value is in 'EM' units. Typical values
2996 * for slight expansion will be around 0.05. Negative values tighten text.
2998 * @see #getLetterSpacing()
2999 * @see Paint#getLetterSpacing
3001 * @attr ref android.R.styleable#TextView_letterSpacing
3003 @android.view.RemotableViewMethod
3004 public void setLetterSpacing(float letterSpacing) {
3005 if (letterSpacing != mTextPaint.getLetterSpacing()) {
3006 mTextPaint.setLetterSpacing(letterSpacing);
3008 if (mLayout != null) {
3017 * @return the currently set font feature settings. Default is null.
3019 * @see #setFontFeatureSettings(String)
3020 * @see Paint#setFontFeatureSettings
3023 public String getFontFeatureSettings() {
3024 return mTextPaint.getFontFeatureSettings();
3028 * Sets the break strategy for breaking paragraphs into lines. The default value for
3029 * TextView is {@link Layout#BREAK_STRATEGY_HIGH_QUALITY}, and the default value for
3030 * EditText is {@link Layout#BREAK_STRATEGY_SIMPLE}, the latter to avoid the
3031 * text "dancing" when being edited.
3033 * @attr ref android.R.styleable#TextView_breakStrategy
3034 * @see #getBreakStrategy()
3036 public void setBreakStrategy(@Layout.BreakStrategy int breakStrategy) {
3037 mBreakStrategy = breakStrategy;
3038 if (mLayout != null) {
3046 * @return the currently set break strategy.
3048 * @attr ref android.R.styleable#TextView_breakStrategy
3049 * @see #setBreakStrategy(int)
3051 @Layout.BreakStrategy
3052 public int getBreakStrategy() {
3053 return mBreakStrategy;
3057 * Sets the hyphenation frequency. The default value for both TextView and EditText, which is set
3058 * from the theme, is {@link Layout#HYPHENATION_FREQUENCY_NORMAL}.
3060 * @attr ref android.R.styleable#TextView_hyphenationFrequency
3061 * @see #getHyphenationFrequency()
3063 public void setHyphenationFrequency(@Layout.HyphenationFrequency int hyphenationFrequency) {
3064 mHyphenationFrequency = hyphenationFrequency;
3065 if (mLayout != null) {
3073 * @return the currently set hyphenation frequency.
3075 * @attr ref android.R.styleable#TextView_hyphenationFrequency
3076 * @see #setHyphenationFrequency(int)
3078 @Layout.HyphenationFrequency
3079 public int getHyphenationFrequency() {
3080 return mHyphenationFrequency;
3084 * Sets font feature settings. The format is the same as the CSS
3085 * font-feature-settings attribute:
3086 * http://dev.w3.org/csswg/css-fonts/#propdef-font-feature-settings
3088 * @param fontFeatureSettings font feature settings represented as CSS compatible string
3089 * @see #getFontFeatureSettings()
3090 * @see Paint#getFontFeatureSettings
3092 * @attr ref android.R.styleable#TextView_fontFeatureSettings
3094 @android.view.RemotableViewMethod
3095 public void setFontFeatureSettings(@Nullable String fontFeatureSettings) {
3096 if (fontFeatureSettings != mTextPaint.getFontFeatureSettings()) {
3097 mTextPaint.setFontFeatureSettings(fontFeatureSettings);
3099 if (mLayout != null) {
3109 * Sets the text color for all the states (normal, selected,
3110 * focused) to be this color.
3112 * @see #setTextColor(ColorStateList)
3113 * @see #getTextColors()
3115 * @attr ref android.R.styleable#TextView_textColor
3117 @android.view.RemotableViewMethod
3118 public void setTextColor(@ColorInt int color) {
3119 mTextColor = ColorStateList.valueOf(color);
3124 * Sets the text color.
3126 * @see #setTextColor(int)
3127 * @see #getTextColors()
3128 * @see #setHintTextColor(ColorStateList)
3129 * @see #setLinkTextColor(ColorStateList)
3131 * @attr ref android.R.styleable#TextView_textColor
3133 public void setTextColor(ColorStateList colors) {
3134 if (colors == null) {
3135 throw new NullPointerException();
3138 mTextColor = colors;
3143 * Gets the text colors for the different states (normal, selected, focused) of the TextView.
3145 * @see #setTextColor(ColorStateList)
3146 * @see #setTextColor(int)
3148 * @attr ref android.R.styleable#TextView_textColor
3150 public final ColorStateList getTextColors() {
3155 * <p>Return the current color selected for normal text.</p>
3157 * @return Returns the current text color.
3160 public final int getCurrentTextColor() {
3161 return mCurTextColor;
3165 * Sets the color used to display the selection highlight.
3167 * @attr ref android.R.styleable#TextView_textColorHighlight
3169 @android.view.RemotableViewMethod
3170 public void setHighlightColor(@ColorInt int color) {
3171 if (mHighlightColor != color) {
3172 mHighlightColor = color;
3178 * @return the color used to display the selection highlight
3180 * @see #setHighlightColor(int)
3182 * @attr ref android.R.styleable#TextView_textColorHighlight
3185 public int getHighlightColor() {
3186 return mHighlightColor;
3190 * Sets whether the soft input method will be made visible when this
3191 * TextView gets focused. The default is true.
3193 @android.view.RemotableViewMethod
3194 public final void setShowSoftInputOnFocus(boolean show) {
3195 createEditorIfNeeded();
3196 mEditor.mShowSoftInputOnFocus = show;
3200 * Returns whether the soft input method will be made visible when this
3201 * TextView gets focused. The default is true.
3203 public final boolean getShowSoftInputOnFocus() {
3204 // When there is no Editor, return default true value
3205 return mEditor == null || mEditor.mShowSoftInputOnFocus;
3209 * Gives the text a shadow of the specified blur radius and color, the specified
3210 * distance from its drawn position.
3212 * The text shadow produced does not interact with the properties on view
3213 * that are responsible for real time shadows,
3214 * {@link View#getElevation() elevation} and
3215 * {@link View#getTranslationZ() translationZ}.
3217 * @see Paint#setShadowLayer(float, float, float, int)
3219 * @attr ref android.R.styleable#TextView_shadowColor
3220 * @attr ref android.R.styleable#TextView_shadowDx
3221 * @attr ref android.R.styleable#TextView_shadowDy
3222 * @attr ref android.R.styleable#TextView_shadowRadius
3224 public void setShadowLayer(float radius, float dx, float dy, int color) {
3225 mTextPaint.setShadowLayer(radius, dx, dy, color);
3227 mShadowRadius = radius;
3230 mShadowColor = color;
3232 // Will change text clip region
3233 if (mEditor != null) mEditor.invalidateTextDisplayList();
3238 * Gets the radius of the shadow layer.
3240 * @return the radius of the shadow layer. If 0, the shadow layer is not visible
3242 * @see #setShadowLayer(float, float, float, int)
3244 * @attr ref android.R.styleable#TextView_shadowRadius
3246 public float getShadowRadius() {
3247 return mShadowRadius;
3251 * @return the horizontal offset of the shadow layer
3253 * @see #setShadowLayer(float, float, float, int)
3255 * @attr ref android.R.styleable#TextView_shadowDx
3257 public float getShadowDx() {
3262 * @return the vertical offset of the shadow layer
3264 * @see #setShadowLayer(float, float, float, int)
3266 * @attr ref android.R.styleable#TextView_shadowDy
3268 public float getShadowDy() {
3273 * @return the color of the shadow layer
3275 * @see #setShadowLayer(float, float, float, int)
3277 * @attr ref android.R.styleable#TextView_shadowColor
3280 public int getShadowColor() {
3281 return mShadowColor;
3285 * @return the base paint used for the text. Please use this only to
3286 * consult the Paint's properties and not to change them.
3288 public TextPaint getPaint() {
3293 * Sets the autolink mask of the text. See {@link
3294 * android.text.util.Linkify#ALL Linkify.ALL} and peers for
3297 * @attr ref android.R.styleable#TextView_autoLink
3299 @android.view.RemotableViewMethod
3300 public final void setAutoLinkMask(int mask) {
3301 mAutoLinkMask = mask;
3305 * Sets whether the movement method will automatically be set to
3306 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been
3307 * set to nonzero and links are detected in {@link #setText}.
3308 * The default is true.
3310 * @attr ref android.R.styleable#TextView_linksClickable
3312 @android.view.RemotableViewMethod
3313 public final void setLinksClickable(boolean whether) {
3314 mLinksClickable = whether;
3318 * Returns whether the movement method will automatically be set to
3319 * {@link LinkMovementMethod} if {@link #setAutoLinkMask} has been
3320 * set to nonzero and links are detected in {@link #setText}.
3321 * The default is true.
3323 * @attr ref android.R.styleable#TextView_linksClickable
3325 public final boolean getLinksClickable() {
3326 return mLinksClickable;
3330 * Returns the list of URLSpans attached to the text
3331 * (by {@link Linkify} or otherwise) if any. You can call
3332 * {@link URLSpan#getURL} on them to find where they link to
3333 * or use {@link Spanned#getSpanStart} and {@link Spanned#getSpanEnd}
3334 * to find the region of the text they are attached to.
3336 public URLSpan[] getUrls() {
3337 if (mText instanceof Spanned) {
3338 return ((Spanned) mText).getSpans(0, mText.length(), URLSpan.class);
3340 return new URLSpan[0];
3345 * Sets the color of the hint text for all the states (disabled, focussed, selected...) of this
3348 * @see #setHintTextColor(ColorStateList)
3349 * @see #getHintTextColors()
3350 * @see #setTextColor(int)
3352 * @attr ref android.R.styleable#TextView_textColorHint
3354 @android.view.RemotableViewMethod
3355 public final void setHintTextColor(@ColorInt int color) {
3356 mHintTextColor = ColorStateList.valueOf(color);
3361 * Sets the color of the hint text.
3363 * @see #getHintTextColors()
3364 * @see #setHintTextColor(int)
3365 * @see #setTextColor(ColorStateList)
3366 * @see #setLinkTextColor(ColorStateList)
3368 * @attr ref android.R.styleable#TextView_textColorHint
3370 public final void setHintTextColor(ColorStateList colors) {
3371 mHintTextColor = colors;
3376 * @return the color of the hint text, for the different states of this TextView.
3378 * @see #setHintTextColor(ColorStateList)
3379 * @see #setHintTextColor(int)
3380 * @see #setTextColor(ColorStateList)
3381 * @see #setLinkTextColor(ColorStateList)
3383 * @attr ref android.R.styleable#TextView_textColorHint
3385 public final ColorStateList getHintTextColors() {
3386 return mHintTextColor;
3390 * <p>Return the current color selected to paint the hint text.</p>
3392 * @return Returns the current hint text color.
3395 public final int getCurrentHintTextColor() {
3396 return mHintTextColor != null ? mCurHintTextColor : mCurTextColor;
3400 * Sets the color of links in the text.
3402 * @see #setLinkTextColor(ColorStateList)
3403 * @see #getLinkTextColors()
3405 * @attr ref android.R.styleable#TextView_textColorLink
3407 @android.view.RemotableViewMethod
3408 public final void setLinkTextColor(@ColorInt int color) {
3409 mLinkTextColor = ColorStateList.valueOf(color);
3414 * Sets the color of links in the text.
3416 * @see #setLinkTextColor(int)
3417 * @see #getLinkTextColors()
3418 * @see #setTextColor(ColorStateList)
3419 * @see #setHintTextColor(ColorStateList)
3421 * @attr ref android.R.styleable#TextView_textColorLink
3423 public final void setLinkTextColor(ColorStateList colors) {
3424 mLinkTextColor = colors;
3429 * @return the list of colors used to paint the links in the text, for the different states of
3432 * @see #setLinkTextColor(ColorStateList)
3433 * @see #setLinkTextColor(int)
3435 * @attr ref android.R.styleable#TextView_textColorLink
3437 public final ColorStateList getLinkTextColors() {
3438 return mLinkTextColor;
3442 * Sets the horizontal alignment of the text and the
3443 * vertical gravity that will be used when there is extra space
3444 * in the TextView beyond what is required for the text itself.
3446 * @see android.view.Gravity
3447 * @attr ref android.R.styleable#TextView_gravity
3449 public void setGravity(int gravity) {
3450 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
3451 gravity |= Gravity.START;
3453 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
3454 gravity |= Gravity.TOP;
3457 boolean newLayout = false;
3459 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) !=
3460 (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK)) {
3464 if (gravity != mGravity) {
3470 if (mLayout != null && newLayout) {
3471 // XXX this is heavy-handed because no actual content changes.
3472 int want = mLayout.getWidth();
3473 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();
3475 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
3476 mRight - mLeft - getCompoundPaddingLeft() -
3477 getCompoundPaddingRight(), true);
3482 * Returns the horizontal and vertical alignment of this TextView.
3484 * @see android.view.Gravity
3485 * @attr ref android.R.styleable#TextView_gravity
3487 public int getGravity() {
3492 * @return the flags on the Paint being used to display the text.
3493 * @see Paint#getFlags
3495 public int getPaintFlags() {
3496 return mTextPaint.getFlags();
3500 * Sets flags on the Paint being used to display the text and
3501 * reflows the text if they are different from the old flags.
3502 * @see Paint#setFlags
3504 @android.view.RemotableViewMethod
3505 public void setPaintFlags(int flags) {
3506 if (mTextPaint.getFlags() != flags) {
3507 mTextPaint.setFlags(flags);
3509 if (mLayout != null) {
3518 * Sets whether the text should be allowed to be wider than the
3519 * View is. If false, it will be wrapped to the width of the View.
3521 * @attr ref android.R.styleable#TextView_scrollHorizontally
3523 public void setHorizontallyScrolling(boolean whether) {
3524 if (mHorizontallyScrolling != whether) {
3525 mHorizontallyScrolling = whether;
3527 if (mLayout != null) {
3536 * Returns whether the text is allowed to be wider than the View is.
3537 * If false, the text will be wrapped to the width of the View.
3539 * @attr ref android.R.styleable#TextView_scrollHorizontally
3542 public boolean getHorizontallyScrolling() {
3543 return mHorizontallyScrolling;
3547 * Makes the TextView at least this many lines tall.
3549 * Setting this value overrides any other (minimum) height setting. A single line TextView will
3550 * set this value to 1.
3552 * @see #getMinLines()
3554 * @attr ref android.R.styleable#TextView_minLines
3556 @android.view.RemotableViewMethod
3557 public void setMinLines(int minlines) {
3558 mMinimum = minlines;
3566 * @return the minimum number of lines displayed in this TextView, or -1 if the minimum
3567 * height was set in pixels instead using {@link #setMinHeight(int) or #setHeight(int)}.
3569 * @see #setMinLines(int)
3571 * @attr ref android.R.styleable#TextView_minLines
3573 public int getMinLines() {
3574 return mMinMode == LINES ? mMinimum : -1;
3578 * Makes the TextView at least this many pixels tall.
3580 * Setting this value overrides any other (minimum) number of lines setting.
3582 * @attr ref android.R.styleable#TextView_minHeight
3584 @android.view.RemotableViewMethod
3585 public void setMinHeight(int minHeight) {
3586 mMinimum = minHeight;
3594 * @return the minimum height of this TextView expressed in pixels, or -1 if the minimum
3595 * height was set in number of lines instead using {@link #setMinLines(int) or #setLines(int)}.
3597 * @see #setMinHeight(int)
3599 * @attr ref android.R.styleable#TextView_minHeight
3601 public int getMinHeight() {
3602 return mMinMode == PIXELS ? mMinimum : -1;
3606 * Makes the TextView at most this many lines tall.
3608 * Setting this value overrides any other (maximum) height setting.
3610 * @attr ref android.R.styleable#TextView_maxLines
3612 @android.view.RemotableViewMethod
3613 public void setMaxLines(int maxlines) {
3614 mMaximum = maxlines;
3622 * @return the maximum number of lines displayed in this TextView, or -1 if the maximum
3623 * height was set in pixels instead using {@link #setMaxHeight(int) or #setHeight(int)}.
3625 * @see #setMaxLines(int)
3627 * @attr ref android.R.styleable#TextView_maxLines
3629 public int getMaxLines() {
3630 return mMaxMode == LINES ? mMaximum : -1;
3634 * Makes the TextView at most this many pixels tall. This option is mutually exclusive with the
3635 * {@link #setMaxLines(int)} method.
3637 * Setting this value overrides any other (maximum) number of lines setting.
3639 * @attr ref android.R.styleable#TextView_maxHeight
3641 @android.view.RemotableViewMethod
3642 public void setMaxHeight(int maxHeight) {
3643 mMaximum = maxHeight;
3651 * @return the maximum height of this TextView expressed in pixels, or -1 if the maximum
3652 * height was set in number of lines instead using {@link #setMaxLines(int) or #setLines(int)}.
3654 * @see #setMaxHeight(int)
3656 * @attr ref android.R.styleable#TextView_maxHeight
3658 public int getMaxHeight() {
3659 return mMaxMode == PIXELS ? mMaximum : -1;
3663 * Makes the TextView exactly this many lines tall.
3665 * Note that setting this value overrides any other (minimum / maximum) number of lines or
3666 * height setting. A single line TextView will set this value to 1.
3668 * @attr ref android.R.styleable#TextView_lines
3670 @android.view.RemotableViewMethod
3671 public void setLines(int lines) {
3672 mMaximum = mMinimum = lines;
3673 mMaxMode = mMinMode = LINES;
3680 * Makes the TextView exactly this many pixels tall.
3681 * You could do the same thing by specifying this number in the
3684 * Note that setting this value overrides any other (minimum / maximum) number of lines or
3687 * @attr ref android.R.styleable#TextView_height
3689 @android.view.RemotableViewMethod
3690 public void setHeight(int pixels) {
3691 mMaximum = mMinimum = pixels;
3692 mMaxMode = mMinMode = PIXELS;
3699 * Makes the TextView at least this many ems wide
3701 * @attr ref android.R.styleable#TextView_minEms
3703 @android.view.RemotableViewMethod
3704 public void setMinEms(int minems) {
3706 mMinWidthMode = EMS;
3713 * @return the minimum width of the TextView, expressed in ems or -1 if the minimum width
3714 * was set in pixels instead (using {@link #setMinWidth(int)} or {@link #setWidth(int)}).
3716 * @see #setMinEms(int)
3719 * @attr ref android.R.styleable#TextView_minEms
3721 public int getMinEms() {
3722 return mMinWidthMode == EMS ? mMinWidth : -1;
3726 * Makes the TextView at least this many pixels wide
3728 * @attr ref android.R.styleable#TextView_minWidth
3730 @android.view.RemotableViewMethod
3731 public void setMinWidth(int minpixels) {
3732 mMinWidth = minpixels;
3733 mMinWidthMode = PIXELS;
3740 * @return the minimum width of the TextView, in pixels or -1 if the minimum width
3741 * was set in ems instead (using {@link #setMinEms(int)} or {@link #setEms(int)}).
3743 * @see #setMinWidth(int)
3744 * @see #setWidth(int)
3746 * @attr ref android.R.styleable#TextView_minWidth
3748 public int getMinWidth() {
3749 return mMinWidthMode == PIXELS ? mMinWidth : -1;
3753 * Makes the TextView at most this many ems wide
3755 * @attr ref android.R.styleable#TextView_maxEms
3757 @android.view.RemotableViewMethod
3758 public void setMaxEms(int maxems) {
3760 mMaxWidthMode = EMS;
3767 * @return the maximum width of the TextView, expressed in ems or -1 if the maximum width
3768 * was set in pixels instead (using {@link #setMaxWidth(int)} or {@link #setWidth(int)}).
3770 * @see #setMaxEms(int)
3773 * @attr ref android.R.styleable#TextView_maxEms
3775 public int getMaxEms() {
3776 return mMaxWidthMode == EMS ? mMaxWidth : -1;
3780 * Makes the TextView at most this many pixels wide
3782 * @attr ref android.R.styleable#TextView_maxWidth
3784 @android.view.RemotableViewMethod
3785 public void setMaxWidth(int maxpixels) {
3786 mMaxWidth = maxpixels;
3787 mMaxWidthMode = PIXELS;
3794 * @return the maximum width of the TextView, in pixels or -1 if the maximum width
3795 * was set in ems instead (using {@link #setMaxEms(int)} or {@link #setEms(int)}).
3797 * @see #setMaxWidth(int)
3798 * @see #setWidth(int)
3800 * @attr ref android.R.styleable#TextView_maxWidth
3802 public int getMaxWidth() {
3803 return mMaxWidthMode == PIXELS ? mMaxWidth : -1;
3807 * Makes the TextView exactly this many ems wide
3809 * @see #setMaxEms(int)
3810 * @see #setMinEms(int)
3814 * @attr ref android.R.styleable#TextView_ems
3816 @android.view.RemotableViewMethod
3817 public void setEms(int ems) {
3818 mMaxWidth = mMinWidth = ems;
3819 mMaxWidthMode = mMinWidthMode = EMS;
3826 * Makes the TextView exactly this many pixels wide.
3827 * You could do the same thing by specifying this number in the
3830 * @see #setMaxWidth(int)
3831 * @see #setMinWidth(int)
3832 * @see #getMinWidth()
3833 * @see #getMaxWidth()
3835 * @attr ref android.R.styleable#TextView_width
3837 @android.view.RemotableViewMethod
3838 public void setWidth(int pixels) {
3839 mMaxWidth = mMinWidth = pixels;
3840 mMaxWidthMode = mMinWidthMode = PIXELS;
3847 * Sets line spacing for this TextView. Each line will have its height
3848 * multiplied by <code>mult</code> and have <code>add</code> added to it.
3850 * @attr ref android.R.styleable#TextView_lineSpacingExtra
3851 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
3853 public void setLineSpacing(float add, float mult) {
3854 if (mSpacingAdd != add || mSpacingMult != mult) {
3856 mSpacingMult = mult;
3858 if (mLayout != null) {
3867 * Gets the line spacing multiplier
3869 * @return the value by which each line's height is multiplied to get its actual height.
3871 * @see #setLineSpacing(float, float)
3872 * @see #getLineSpacingExtra()
3874 * @attr ref android.R.styleable#TextView_lineSpacingMultiplier
3876 public float getLineSpacingMultiplier() {
3877 return mSpacingMult;
3881 * Gets the line spacing extra space
3883 * @return the extra space that is added to the height of each lines of this TextView.
3885 * @see #setLineSpacing(float, float)
3886 * @see #getLineSpacingMultiplier()
3888 * @attr ref android.R.styleable#TextView_lineSpacingExtra
3890 public float getLineSpacingExtra() {
3895 * Convenience method: Append the specified text to the TextView's
3896 * display buffer, upgrading it to BufferType.EDITABLE if it was
3897 * not already editable.
3899 public final void append(CharSequence text) {
3900 append(text, 0, text.length());
3904 * Convenience method: Append the specified text slice to the TextView's
3905 * display buffer, upgrading it to BufferType.EDITABLE if it was
3906 * not already editable.
3908 public void append(CharSequence text, int start, int end) {
3909 if (!(mText instanceof Editable)) {
3910 setText(mText, BufferType.EDITABLE);
3913 ((Editable) mText).append(text, start, end);
3916 private void updateTextColors() {
3917 boolean inval = false;
3918 int color = mTextColor.getColorForState(getDrawableState(), 0);
3919 if (color != mCurTextColor) {
3920 mCurTextColor = color;
3923 if (mLinkTextColor != null) {
3924 color = mLinkTextColor.getColorForState(getDrawableState(), 0);
3925 if (color != mTextPaint.linkColor) {
3926 mTextPaint.linkColor = color;
3930 if (mHintTextColor != null) {
3931 color = mHintTextColor.getColorForState(getDrawableState(), 0);
3932 if (color != mCurHintTextColor) {
3933 mCurHintTextColor = color;
3934 if (mText.length() == 0) {
3940 // Text needs to be redrawn with the new color
3941 if (mEditor != null) mEditor.invalidateTextDisplayList();
3947 protected void drawableStateChanged() {
3948 super.drawableStateChanged();
3949 if (mTextColor != null && mTextColor.isStateful()
3950 || (mHintTextColor != null && mHintTextColor.isStateful())
3951 || (mLinkTextColor != null && mLinkTextColor.isStateful())) {
3955 if (mDrawables != null) {
3956 final int[] state = getDrawableState();
3957 for (Drawable dr : mDrawables.mShowing) {
3958 if (dr != null && dr.isStateful()) {
3966 public void drawableHotspotChanged(float x, float y) {
3967 super.drawableHotspotChanged(x, y);
3969 if (mDrawables != null) {
3970 final int[] state = getDrawableState();
3971 for (Drawable dr : mDrawables.mShowing) {
3972 if (dr != null && dr.isStateful()) {
3973 dr.setHotspot(x, y);
3980 public Parcelable onSaveInstanceState() {
3981 Parcelable superState = super.onSaveInstanceState();
3983 // Save state if we are forced to
3984 boolean save = mFreezesText;
3988 if (mText != null) {
3989 start = getSelectionStart();
3990 end = getSelectionEnd();
3991 if (start >= 0 || end >= 0) {
3992 // Or save state if there is a selection
3998 SavedState ss = new SavedState(superState);
3999 // XXX Should also save the current scroll position!
4000 ss.selStart = start;
4003 if (mText instanceof Spanned) {
4004 Spannable sp = new SpannableStringBuilder(mText);
4006 if (mEditor != null) {
4007 removeMisspelledSpans(sp);
4008 sp.removeSpan(mEditor.mSuggestionRangeSpan);
4013 ss.text = mText.toString();
4016 if (isFocused() && start >= 0 && end >= 0) {
4017 ss.frozenWithFocus = true;
4020 ss.error = getError();
4022 if (mEditor != null) {
4023 ss.editorState = mEditor.saveInstanceState();
4031 void removeMisspelledSpans(Spannable spannable) {
4032 SuggestionSpan[] suggestionSpans = spannable.getSpans(0, spannable.length(),
4033 SuggestionSpan.class);
4034 for (int i = 0; i < suggestionSpans.length; i++) {
4035 int flags = suggestionSpans[i].getFlags();
4036 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
4037 && (flags & SuggestionSpan.FLAG_MISSPELLED) != 0) {
4038 spannable.removeSpan(suggestionSpans[i]);
4044 public void onRestoreInstanceState(Parcelable state) {
4045 if (!(state instanceof SavedState)) {
4046 super.onRestoreInstanceState(state);
4050 SavedState ss = (SavedState)state;
4051 super.onRestoreInstanceState(ss.getSuperState());
4053 // XXX restore buffer type too, as well as lots of other stuff
4054 if (ss.text != null) {
4058 if (ss.selStart >= 0 && ss.selEnd >= 0) {
4059 if (mText instanceof Spannable) {
4060 int len = mText.length();
4062 if (ss.selStart > len || ss.selEnd > len) {
4063 String restored = "";
4065 if (ss.text != null) {
4066 restored = "(restored) ";
4069 Log.e(LOG_TAG, "Saved cursor position " + ss.selStart +
4070 "/" + ss.selEnd + " out of range for " + restored +
4073 Selection.setSelection((Spannable) mText, ss.selStart, ss.selEnd);
4075 if (ss.frozenWithFocus) {
4076 createEditorIfNeeded();
4077 mEditor.mFrozenWithFocus = true;
4083 if (ss.error != null) {
4084 final CharSequence error = ss.error;
4085 // Display the error later, after the first layout pass
4086 post(new Runnable() {
4088 if (mEditor == null || !mEditor.mErrorWasChanged) {
4095 if (ss.editorState != null) {
4096 createEditorIfNeeded();
4097 mEditor.restoreInstanceState(ss.editorState);
4102 * Control whether this text view saves its entire text contents when
4103 * freezing to an icicle, in addition to dynamic state such as cursor
4104 * position. By default this is false, not saving the text. Set to true
4105 * if the text in the text view is not being saved somewhere else in
4106 * persistent storage (such as in a content provider) so that if the
4107 * view is later thawed the user will not lose their data.
4109 * @param freezesText Controls whether a frozen icicle should include the
4110 * entire text data: true to include it, false to not.
4112 * @attr ref android.R.styleable#TextView_freezesText
4114 @android.view.RemotableViewMethod
4115 public void setFreezesText(boolean freezesText) {
4116 mFreezesText = freezesText;
4120 * Return whether this text view is including its entire text contents
4121 * in frozen icicles.
4123 * @return Returns true if text is included, false if it isn't.
4125 * @see #setFreezesText
4127 public boolean getFreezesText() {
4128 return mFreezesText;
4131 ///////////////////////////////////////////////////////////////////////////
4134 * Sets the Factory used to create new Editables.
4136 public final void setEditableFactory(Editable.Factory factory) {
4137 mEditableFactory = factory;
4142 * Sets the Factory used to create new Spannables.
4144 public final void setSpannableFactory(Spannable.Factory factory) {
4145 mSpannableFactory = factory;
4150 * Sets the string value of the TextView. TextView <em>does not</em> accept
4151 * HTML-like formatting, which you can do with text strings in XML resource files.
4152 * To style your strings, attach android.text.style.* objects to a
4153 * {@link android.text.SpannableString SpannableString}, or see the
4154 * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources">
4155 * Available Resource Types</a> documentation for an example of setting
4156 * formatted text in the XML resource file.
4158 * @attr ref android.R.styleable#TextView_text
4160 @android.view.RemotableViewMethod
4161 public final void setText(CharSequence text) {
4162 setText(text, mBufferType);
4166 * Like {@link #setText(CharSequence)},
4167 * except that the cursor position (if any) is retained in the new text.
4169 * @param text The new text to place in the text view.
4171 * @see #setText(CharSequence)
4173 @android.view.RemotableViewMethod
4174 public final void setTextKeepState(CharSequence text) {
4175 setTextKeepState(text, mBufferType);
4179 * Sets the text that this TextView is to display (see
4180 * {@link #setText(CharSequence)}) and also sets whether it is stored
4181 * in a styleable/spannable buffer and whether it is editable.
4183 * @attr ref android.R.styleable#TextView_text
4184 * @attr ref android.R.styleable#TextView_bufferType
4186 public void setText(CharSequence text, BufferType type) {
4187 setText(text, type, true, 0);
4189 if (mCharWrapper != null) {
4190 mCharWrapper.mChars = null;
4194 private void setText(CharSequence text, BufferType type,
4195 boolean notifyBefore, int oldlen) {
4200 // If suggestions are not enabled, remove the suggestion spans from the text
4201 if (!isSuggestionsEnabled()) {
4202 text = removeSuggestionSpans(text);
4205 if (!mUserSetTextScaleX) mTextPaint.setTextScaleX(1.0f);
4207 if (text instanceof Spanned &&
4208 ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
4209 if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) {
4210 setHorizontalFadingEdgeEnabled(true);
4211 mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
4213 setHorizontalFadingEdgeEnabled(false);
4214 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
4216 setEllipsize(TextUtils.TruncateAt.MARQUEE);
4219 int n = mFilters.length;
4220 for (int i = 0; i < n; i++) {
4221 CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
4228 if (mText != null) {
4229 oldlen = mText.length();
4230 sendBeforeTextChanged(mText, 0, oldlen, text.length());
4232 sendBeforeTextChanged("", 0, 0, text.length());
4236 boolean needEditableForNotification = false;
4238 if (mListeners != null && mListeners.size() != 0) {
4239 needEditableForNotification = true;
4242 if (type == BufferType.EDITABLE || getKeyListener() != null ||
4243 needEditableForNotification) {
4244 createEditorIfNeeded();
4245 mEditor.forgetUndoRedo();
4246 Editable t = mEditableFactory.newEditable(text);
4248 setFilters(t, mFilters);
4249 InputMethodManager imm = InputMethodManager.peekInstance();
4250 if (imm != null) imm.restartInput(this);
4251 } else if (type == BufferType.SPANNABLE || mMovement != null) {
4252 text = mSpannableFactory.newSpannable(text);
4253 } else if (!(text instanceof CharWrapper)) {
4254 text = TextUtils.stringOrSpannedString(text);
4257 if (mAutoLinkMask != 0) {
4260 if (type == BufferType.EDITABLE || text instanceof Spannable) {
4261 s2 = (Spannable) text;
4263 s2 = mSpannableFactory.newSpannable(text);
4266 if (Linkify.addLinks(s2, mAutoLinkMask)) {
4268 type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;
4271 * We must go ahead and set the text before changing the
4272 * movement method, because setMovementMethod() may call
4273 * setText() again to try to upgrade the buffer type.
4277 // Do not change the movement method for text that support text selection as it
4278 // would prevent an arbitrary cursor displacement.
4279 if (mLinksClickable && !textCanBeSelected()) {
4280 setMovementMethod(LinkMovementMethod.getInstance());
4288 if (mTransformation == null) {
4289 mTransformed = text;
4291 mTransformed = mTransformation.getTransformation(text, this);
4294 final int textLength = text.length();
4296 if (text instanceof Spannable && !mAllowTransformationLengthChange) {
4297 Spannable sp = (Spannable) text;
4299 // Remove any ChangeWatchers that might have come from other TextViews.
4300 final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
4301 final int count = watchers.length;
4302 for (int i = 0; i < count; i++) {
4303 sp.removeSpan(watchers[i]);
4306 if (mChangeWatcher == null) mChangeWatcher = new ChangeWatcher();
4308 sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE |
4309 (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));
4311 if (mEditor != null) mEditor.addSpanWatchers(sp);
4313 if (mTransformation != null) {
4314 sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
4317 if (mMovement != null) {
4318 mMovement.initialize(this, (Spannable) text);
4321 * Initializing the movement method will have set the
4322 * selection, so reset mSelectionMoved to keep that from
4323 * interfering with the normal on-focus selection-setting.
4325 if (mEditor != null) mEditor.mSelectionMoved = false;
4329 if (mLayout != null) {
4333 sendOnTextChanged(text, 0, oldlen, textLength);
4334 onTextChanged(text, 0, oldlen, textLength);
4336 notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
4338 if (needEditableForNotification) {
4339 sendAfterTextChanged((Editable) text);
4342 // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
4343 if (mEditor != null) mEditor.prepareCursorControllers();
4347 * Sets the TextView to display the specified slice of the specified
4348 * char array. You must promise that you will not change the contents
4349 * of the array except for right before another call to setText(),
4350 * since the TextView has no way to know that the text
4351 * has changed and that it needs to invalidate and re-layout.
4353 public final void setText(char[] text, int start, int len) {
4356 if (start < 0 || len < 0 || start + len > text.length) {
4357 throw new IndexOutOfBoundsException(start + ", " + len);
4361 * We must do the before-notification here ourselves because if
4362 * the old text is a CharWrapper we destroy it before calling
4363 * into the normal path.
4365 if (mText != null) {
4366 oldlen = mText.length();
4367 sendBeforeTextChanged(mText, 0, oldlen, len);
4369 sendBeforeTextChanged("", 0, 0, len);
4372 if (mCharWrapper == null) {
4373 mCharWrapper = new CharWrapper(text, start, len);
4375 mCharWrapper.set(text, start, len);
4378 setText(mCharWrapper, mBufferType, false, oldlen);
4382 * Like {@link #setText(CharSequence, android.widget.TextView.BufferType)},
4383 * except that the cursor position (if any) is retained in the new text.
4385 * @see #setText(CharSequence, android.widget.TextView.BufferType)
4387 public final void setTextKeepState(CharSequence text, BufferType type) {
4388 int start = getSelectionStart();
4389 int end = getSelectionEnd();
4390 int len = text.length();
4392 setText(text, type);
4394 if (start >= 0 || end >= 0) {
4395 if (mText instanceof Spannable) {
4396 Selection.setSelection((Spannable) mText,
4397 Math.max(0, Math.min(start, len)),
4398 Math.max(0, Math.min(end, len)));
4403 @android.view.RemotableViewMethod
4404 public final void setText(@StringRes int resid) {
4405 setText(getContext().getResources().getText(resid));
4408 public final void setText(@StringRes int resid, BufferType type) {
4409 setText(getContext().getResources().getText(resid), type);
4413 * Sets the text to be displayed when the text of the TextView is empty.
4414 * Null means to use the normal empty text. The hint does not currently
4415 * participate in determining the size of the view.
4417 * @attr ref android.R.styleable#TextView_hint
4419 @android.view.RemotableViewMethod
4420 public final void setHint(CharSequence hint) {
4421 mHint = TextUtils.stringOrSpannedString(hint);
4423 if (mLayout != null) {
4427 if (mText.length() == 0) {
4431 // Invalidate display list if hint is currently used
4432 if (mEditor != null && mText.length() == 0 && mHint != null) {
4433 mEditor.invalidateTextDisplayList();
4438 * Sets the text to be displayed when the text of the TextView is empty,
4441 * @attr ref android.R.styleable#TextView_hint
4443 @android.view.RemotableViewMethod
4444 public final void setHint(@StringRes int resid) {
4445 setHint(getContext().getResources().getText(resid));
4449 * Returns the hint that is displayed when the text of the TextView
4452 * @attr ref android.R.styleable#TextView_hint
4454 @ViewDebug.CapturedViewProperty
4455 public CharSequence getHint() {
4459 boolean isSingleLine() {
4463 private static boolean isMultilineInputType(int type) {
4464 return (type & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)) ==
4465 (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
4469 * Removes the suggestion spans.
4471 CharSequence removeSuggestionSpans(CharSequence text) {
4472 if (text instanceof Spanned) {
4473 Spannable spannable;
4474 if (text instanceof Spannable) {
4475 spannable = (Spannable) text;
4477 spannable = new SpannableString(text);
4481 SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
4482 for (int i = 0; i < spans.length; i++) {
4483 spannable.removeSpan(spans[i]);
4490 * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This
4491 * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)},
4492 * to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL}
4493 * then a soft keyboard will not be displayed for this text view.
4495 * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be
4496 * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input
4499 * @see #getInputType()
4500 * @see #setRawInputType(int)
4501 * @see android.text.InputType
4502 * @attr ref android.R.styleable#TextView_inputType
4504 public void setInputType(int type) {
4505 final boolean wasPassword = isPasswordInputType(getInputType());
4506 final boolean wasVisiblePassword = isVisiblePasswordInputType(getInputType());
4507 setInputType(type, false);
4508 final boolean isPassword = isPasswordInputType(type);
4509 final boolean isVisiblePassword = isVisiblePasswordInputType(type);
4510 boolean forceUpdate = false;
4512 setTransformationMethod(PasswordTransformationMethod.getInstance());
4513 setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
4514 } else if (isVisiblePassword) {
4515 if (mTransformation == PasswordTransformationMethod.getInstance()) {
4518 setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
4519 } else if (wasPassword || wasVisiblePassword) {
4520 // not in password mode, clean up typeface and transformation
4521 setTypefaceFromAttrs(null /* fontFamily */, -1, -1);
4522 if (mTransformation == PasswordTransformationMethod.getInstance()) {
4527 boolean singleLine = !isMultilineInputType(type);
4529 // We need to update the single line mode if it has changed or we
4530 // were previously in password mode.
4531 if (mSingleLine != singleLine || forceUpdate) {
4532 // Change single line mode, but only change the transformation if
4533 // we are not in password mode.
4534 applySingleLine(singleLine, !isPassword, true);
4537 if (!isSuggestionsEnabled()) {
4538 mText = removeSuggestionSpans(mText);
4541 InputMethodManager imm = InputMethodManager.peekInstance();
4542 if (imm != null) imm.restartInput(this);
4546 * It would be better to rely on the input type for everything. A password inputType should have
4547 * a password transformation. We should hence use isPasswordInputType instead of this method.
4550 * - Call setInputType in setKeyListener instead of changing the input type directly (which
4551 * would install the correct transformation).
4552 * - Refuse the installation of a non-password transformation in setTransformation if the input
4555 * However, this is like this for legacy reasons and we cannot break existing apps. This method
4556 * is useful since it matches what the user can see (obfuscated text or not).
4558 * @return true if the current transformation method is of the password type.
4560 boolean hasPasswordTransformationMethod() {
4561 return mTransformation instanceof PasswordTransformationMethod;
4564 private static boolean isPasswordInputType(int inputType) {
4565 final int variation =
4566 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
4568 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)
4570 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD)
4572 == (EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
4575 private static boolean isVisiblePasswordInputType(int inputType) {
4576 final int variation =
4577 inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION);
4579 == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
4583 * Directly change the content type integer of the text view, without
4584 * modifying any other state.
4585 * @see #setInputType(int)
4586 * @see android.text.InputType
4587 * @attr ref android.R.styleable#TextView_inputType
4589 public void setRawInputType(int type) {
4590 if (type == InputType.TYPE_NULL && mEditor == null) return; //TYPE_NULL is the default value
4591 createEditorIfNeeded();
4592 mEditor.mInputType = type;
4595 private void setInputType(int type, boolean direct) {
4596 final int cls = type & EditorInfo.TYPE_MASK_CLASS;
4598 if (cls == EditorInfo.TYPE_CLASS_TEXT) {
4599 boolean autotext = (type & EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT) != 0;
4600 TextKeyListener.Capitalize cap;
4601 if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) {
4602 cap = TextKeyListener.Capitalize.CHARACTERS;
4603 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS) != 0) {
4604 cap = TextKeyListener.Capitalize.WORDS;
4605 } else if ((type & EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) {
4606 cap = TextKeyListener.Capitalize.SENTENCES;
4608 cap = TextKeyListener.Capitalize.NONE;
4610 input = TextKeyListener.getInstance(autotext, cap);
4611 } else if (cls == EditorInfo.TYPE_CLASS_NUMBER) {
4612 input = DigitsKeyListener.getInstance(
4613 (type & EditorInfo.TYPE_NUMBER_FLAG_SIGNED) != 0,
4614 (type & EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) != 0);
4615 } else if (cls == EditorInfo.TYPE_CLASS_DATETIME) {
4616 switch (type & EditorInfo.TYPE_MASK_VARIATION) {
4617 case EditorInfo.TYPE_DATETIME_VARIATION_DATE:
4618 input = DateKeyListener.getInstance();
4620 case EditorInfo.TYPE_DATETIME_VARIATION_TIME:
4621 input = TimeKeyListener.getInstance();
4624 input = DateTimeKeyListener.getInstance();
4627 } else if (cls == EditorInfo.TYPE_CLASS_PHONE) {
4628 input = DialerKeyListener.getInstance();
4630 input = TextKeyListener.getInstance();
4632 setRawInputType(type);
4634 createEditorIfNeeded();
4635 mEditor.mKeyListener = input;
4637 setKeyListenerOnly(input);
4642 * Get the type of the editable content.
4644 * @see #setInputType(int)
4645 * @see android.text.InputType
4647 public int getInputType() {
4648 return mEditor == null ? EditorInfo.TYPE_NULL : mEditor.mInputType;
4652 * Change the editor type integer associated with the text view, which
4653 * will be reported to an IME with {@link EditorInfo#imeOptions} when it
4655 * @see #getImeOptions
4656 * @see android.view.inputmethod.EditorInfo
4657 * @attr ref android.R.styleable#TextView_imeOptions
4659 public void setImeOptions(int imeOptions) {
4660 createEditorIfNeeded();
4661 mEditor.createInputContentTypeIfNeeded();
4662 mEditor.mInputContentType.imeOptions = imeOptions;
4666 * Get the type of the IME editor.
4668 * @see #setImeOptions(int)
4669 * @see android.view.inputmethod.EditorInfo
4671 public int getImeOptions() {
4672 return mEditor != null && mEditor.mInputContentType != null
4673 ? mEditor.mInputContentType.imeOptions : EditorInfo.IME_NULL;
4677 * Change the custom IME action associated with the text view, which
4678 * will be reported to an IME with {@link EditorInfo#actionLabel}
4679 * and {@link EditorInfo#actionId} when it has focus.
4680 * @see #getImeActionLabel
4681 * @see #getImeActionId
4682 * @see android.view.inputmethod.EditorInfo
4683 * @attr ref android.R.styleable#TextView_imeActionLabel
4684 * @attr ref android.R.styleable#TextView_imeActionId
4686 public void setImeActionLabel(CharSequence label, int actionId) {
4687 createEditorIfNeeded();
4688 mEditor.createInputContentTypeIfNeeded();
4689 mEditor.mInputContentType.imeActionLabel = label;
4690 mEditor.mInputContentType.imeActionId = actionId;
4694 * Get the IME action label previous set with {@link #setImeActionLabel}.
4696 * @see #setImeActionLabel
4697 * @see android.view.inputmethod.EditorInfo
4699 public CharSequence getImeActionLabel() {
4700 return mEditor != null && mEditor.mInputContentType != null
4701 ? mEditor.mInputContentType.imeActionLabel : null;
4705 * Get the IME action ID previous set with {@link #setImeActionLabel}.
4707 * @see #setImeActionLabel
4708 * @see android.view.inputmethod.EditorInfo
4710 public int getImeActionId() {
4711 return mEditor != null && mEditor.mInputContentType != null
4712 ? mEditor.mInputContentType.imeActionId : 0;
4716 * Set a special listener to be called when an action is performed
4717 * on the text view. This will be called when the enter key is pressed,
4718 * or when an action supplied to the IME is selected by the user. Setting
4719 * this means that the normal hard key event will not insert a newline
4720 * into the text view, even if it is multi-line; holding down the ALT
4721 * modifier will, however, allow the user to insert a newline character.
4723 public void setOnEditorActionListener(OnEditorActionListener l) {
4724 createEditorIfNeeded();
4725 mEditor.createInputContentTypeIfNeeded();
4726 mEditor.mInputContentType.onEditorActionListener = l;
4730 * Called when an attached input method calls
4731 * {@link InputConnection#performEditorAction(int)
4732 * InputConnection.performEditorAction()}
4733 * for this text view. The default implementation will call your action
4734 * listener supplied to {@link #setOnEditorActionListener}, or perform
4735 * a standard operation for {@link EditorInfo#IME_ACTION_NEXT
4736 * EditorInfo.IME_ACTION_NEXT}, {@link EditorInfo#IME_ACTION_PREVIOUS
4737 * EditorInfo.IME_ACTION_PREVIOUS}, or {@link EditorInfo#IME_ACTION_DONE
4738 * EditorInfo.IME_ACTION_DONE}.
4740 * <p>For backwards compatibility, if no IME options have been set and the
4741 * text view would not normally advance focus on enter, then
4742 * the NEXT and DONE actions received here will be turned into an enter
4743 * key down/up pair to go through the normal key handling.
4745 * @param actionCode The code of the action being performed.
4747 * @see #setOnEditorActionListener
4749 public void onEditorAction(int actionCode) {
4750 final Editor.InputContentType ict = mEditor == null ? null : mEditor.mInputContentType;
4752 if (ict.onEditorActionListener != null) {
4753 if (ict.onEditorActionListener.onEditorAction(this,
4754 actionCode, null)) {
4759 // This is the handling for some default action.
4760 // Note that for backwards compatibility we don't do this
4761 // default handling if explicit ime options have not been given,
4762 // instead turning this into the normal enter key codes that an
4763 // app may be expecting.
4764 if (actionCode == EditorInfo.IME_ACTION_NEXT) {
4765 View v = focusSearch(FOCUS_FORWARD);
4767 if (!v.requestFocus(FOCUS_FORWARD)) {
4768 throw new IllegalStateException("focus search returned a view " +
4769 "that wasn't able to take focus!");
4774 } else if (actionCode == EditorInfo.IME_ACTION_PREVIOUS) {
4775 View v = focusSearch(FOCUS_BACKWARD);
4777 if (!v.requestFocus(FOCUS_BACKWARD)) {
4778 throw new IllegalStateException("focus search returned a view " +
4779 "that wasn't able to take focus!");
4784 } else if (actionCode == EditorInfo.IME_ACTION_DONE) {
4785 InputMethodManager imm = InputMethodManager.peekInstance();
4786 if (imm != null && imm.isActive(this)) {
4787 imm.hideSoftInputFromWindow(getWindowToken(), 0);
4793 ViewRootImpl viewRootImpl = getViewRootImpl();
4794 if (viewRootImpl != null) {
4795 long eventTime = SystemClock.uptimeMillis();
4796 viewRootImpl.dispatchKeyFromIme(
4797 new KeyEvent(eventTime, eventTime,
4798 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0,
4799 KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
4800 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
4801 | KeyEvent.FLAG_EDITOR_ACTION));
4802 viewRootImpl.dispatchKeyFromIme(
4803 new KeyEvent(SystemClock.uptimeMillis(), eventTime,
4804 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0,
4805 KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
4806 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
4807 | KeyEvent.FLAG_EDITOR_ACTION));
4812 * Set the private content type of the text, which is the
4813 * {@link EditorInfo#privateImeOptions EditorInfo.privateImeOptions}
4814 * field that will be filled in when creating an input connection.
4816 * @see #getPrivateImeOptions()
4817 * @see EditorInfo#privateImeOptions
4818 * @attr ref android.R.styleable#TextView_privateImeOptions
4820 public void setPrivateImeOptions(String type) {
4821 createEditorIfNeeded();
4822 mEditor.createInputContentTypeIfNeeded();
4823 mEditor.mInputContentType.privateImeOptions = type;
4827 * Get the private type of the content.
4829 * @see #setPrivateImeOptions(String)
4830 * @see EditorInfo#privateImeOptions
4832 public String getPrivateImeOptions() {
4833 return mEditor != null && mEditor.mInputContentType != null
4834 ? mEditor.mInputContentType.privateImeOptions : null;
4838 * Set the extra input data of the text, which is the
4839 * {@link EditorInfo#extras TextBoxAttribute.extras}
4840 * Bundle that will be filled in when creating an input connection. The
4841 * given integer is the resource ID of an XML resource holding an
4842 * {@link android.R.styleable#InputExtras <input-extras>} XML tree.
4844 * @see #getInputExtras(boolean)
4845 * @see EditorInfo#extras
4846 * @attr ref android.R.styleable#TextView_editorExtras
4848 public void setInputExtras(@XmlRes int xmlResId) throws XmlPullParserException, IOException {
4849 createEditorIfNeeded();
4850 XmlResourceParser parser = getResources().getXml(xmlResId);
4851 mEditor.createInputContentTypeIfNeeded();
4852 mEditor.mInputContentType.extras = new Bundle();
4853 getResources().parseBundleExtras(parser, mEditor.mInputContentType.extras);
4857 * Retrieve the input extras currently associated with the text view, which
4858 * can be viewed as well as modified.
4860 * @param create If true, the extras will be created if they don't already
4861 * exist. Otherwise, null will be returned if none have been created.
4862 * @see #setInputExtras(int)
4863 * @see EditorInfo#extras
4864 * @attr ref android.R.styleable#TextView_editorExtras
4866 public Bundle getInputExtras(boolean create) {
4867 if (mEditor == null && !create) return null;
4868 createEditorIfNeeded();
4869 if (mEditor.mInputContentType == null) {
4870 if (!create) return null;
4871 mEditor.createInputContentTypeIfNeeded();
4873 if (mEditor.mInputContentType.extras == null) {
4874 if (!create) return null;
4875 mEditor.mInputContentType.extras = new Bundle();
4877 return mEditor.mInputContentType.extras;
4881 * Returns the error message that was set to be displayed with
4882 * {@link #setError}, or <code>null</code> if no error was set
4883 * or if it the error was cleared by the widget after user input.
4885 public CharSequence getError() {
4886 return mEditor == null ? null : mEditor.mError;
4890 * Sets the right-hand compound drawable of the TextView to the "error"
4891 * icon and sets an error message that will be displayed in a popup when
4892 * the TextView has focus. The icon and error message will be reset to
4893 * null when any key events cause changes to the TextView's text. If the
4894 * <code>error</code> is <code>null</code>, the error message and icon
4897 @android.view.RemotableViewMethod
4898 public void setError(CharSequence error) {
4899 if (error == null) {
4900 setError(null, null);
4902 Drawable dr = getContext().getDrawable(
4903 com.android.internal.R.drawable.indicator_input_error);
4905 dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
4906 setError(error, dr);
4911 * Sets the right-hand compound drawable of the TextView to the specified
4912 * icon and sets an error message that will be displayed in a popup when
4913 * the TextView has focus. The icon and error message will be reset to
4914 * null when any key events cause changes to the TextView's text. The
4915 * drawable must already have had {@link Drawable#setBounds} set on it.
4916 * If the <code>error</code> is <code>null</code>, the error message will
4917 * be cleared (and you should provide a <code>null</code> icon as well).
4919 public void setError(CharSequence error, Drawable icon) {
4920 createEditorIfNeeded();
4921 mEditor.setError(error, icon);
4922 notifyViewAccessibilityStateChangedIfNeeded(
4923 AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
4927 protected boolean setFrame(int l, int t, int r, int b) {
4928 boolean result = super.setFrame(l, t, r, b);
4930 if (mEditor != null) mEditor.setFrame();
4932 restartMarqueeIfNeeded();
4937 private void restartMarqueeIfNeeded() {
4938 if (mRestartMarquee && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
4939 mRestartMarquee = false;
4945 * Sets the list of input filters that will be used if the buffer is
4946 * Editable. Has no effect otherwise.
4948 * @attr ref android.R.styleable#TextView_maxLength
4950 public void setFilters(InputFilter[] filters) {
4951 if (filters == null) {
4952 throw new IllegalArgumentException();
4957 if (mText instanceof Editable) {
4958 setFilters((Editable) mText, filters);
4963 * Sets the list of input filters on the specified Editable,
4964 * and includes mInput in the list if it is an InputFilter.
4966 private void setFilters(Editable e, InputFilter[] filters) {
4967 if (mEditor != null) {
4968 final boolean undoFilter = mEditor.mUndoInputFilter != null;
4969 final boolean keyFilter = mEditor.mKeyListener instanceof InputFilter;
4971 if (undoFilter) num++;
4972 if (keyFilter) num++;
4974 InputFilter[] nf = new InputFilter[filters.length + num];
4976 System.arraycopy(filters, 0, nf, 0, filters.length);
4979 nf[filters.length] = mEditor.mUndoInputFilter;
4983 nf[filters.length + num] = (InputFilter) mEditor.mKeyListener;
4990 e.setFilters(filters);
4994 * Returns the current list of input filters.
4996 * @attr ref android.R.styleable#TextView_maxLength
4998 public InputFilter[] getFilters() {
5002 /////////////////////////////////////////////////////////////////////////
5004 private int getBoxHeight(Layout l) {
5005 Insets opticalInsets = isLayoutModeOptical(mParent) ? getOpticalInsets() : Insets.NONE;
5006 int padding = (l == mHintLayout) ?
5007 getCompoundPaddingTop() + getCompoundPaddingBottom() :
5008 getExtendedPaddingTop() + getExtendedPaddingBottom();
5009 return getMeasuredHeight() - padding + opticalInsets.top + opticalInsets.bottom;
5012 int getVerticalOffset(boolean forceNormal) {
5014 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
5017 if (!forceNormal && mText.length() == 0 && mHintLayout != null) {
5021 if (gravity != Gravity.TOP) {
5022 int boxht = getBoxHeight(l);
5023 int textht = l.getHeight();
5025 if (textht < boxht) {
5026 if (gravity == Gravity.BOTTOM)
5027 voffset = boxht - textht;
5028 else // (gravity == Gravity.CENTER_VERTICAL)
5029 voffset = (boxht - textht) >> 1;
5035 private int getBottomVerticalOffset(boolean forceNormal) {
5037 final int gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
5040 if (!forceNormal && mText.length() == 0 && mHintLayout != null) {
5044 if (gravity != Gravity.BOTTOM) {
5045 int boxht = getBoxHeight(l);
5046 int textht = l.getHeight();
5048 if (textht < boxht) {
5049 if (gravity == Gravity.TOP)
5050 voffset = boxht - textht;
5051 else // (gravity == Gravity.CENTER_VERTICAL)
5052 voffset = (boxht - textht) >> 1;
5058 void invalidateCursorPath() {
5059 if (mHighlightPathBogus) {
5062 final int horizontalPadding = getCompoundPaddingLeft();
5063 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true);
5065 if (mEditor.mCursorCount == 0) {
5066 synchronized (TEMP_RECTF) {
5068 * The reason for this concern about the thickness of the
5069 * cursor and doing the floor/ceil on the coordinates is that
5070 * some EditTexts (notably textfields in the Browser) have
5071 * anti-aliased text where not all the characters are
5072 * necessarily at integer-multiple locations. This should
5073 * make sure the entire cursor gets invalidated instead of
5074 * sometimes missing half a pixel.
5076 float thick = (float) Math.ceil(mTextPaint.getStrokeWidth());
5083 // mHighlightPath is guaranteed to be non null at that point.
5084 mHighlightPath.computeBounds(TEMP_RECTF, false);
5086 invalidate((int) Math.floor(horizontalPadding + TEMP_RECTF.left - thick),
5087 (int) Math.floor(verticalPadding + TEMP_RECTF.top - thick),
5088 (int) Math.ceil(horizontalPadding + TEMP_RECTF.right + thick),
5089 (int) Math.ceil(verticalPadding + TEMP_RECTF.bottom + thick));
5092 for (int i = 0; i < mEditor.mCursorCount; i++) {
5093 Rect bounds = mEditor.mCursorDrawable[i].getBounds();
5094 invalidate(bounds.left + horizontalPadding, bounds.top + verticalPadding,
5095 bounds.right + horizontalPadding, bounds.bottom + verticalPadding);
5101 void invalidateCursor() {
5102 int where = getSelectionEnd();
5104 invalidateCursor(where, where, where);
5107 private void invalidateCursor(int a, int b, int c) {
5108 if (a >= 0 || b >= 0 || c >= 0) {
5109 int start = Math.min(Math.min(a, b), c);
5110 int end = Math.max(Math.max(a, b), c);
5111 invalidateRegion(start, end, true /* Also invalidates blinking cursor */);
5116 * Invalidates the region of text enclosed between the start and end text offsets.
5118 void invalidateRegion(int start, int end, boolean invalidateCursor) {
5119 if (mLayout == null) {
5122 int lineStart = mLayout.getLineForOffset(start);
5123 int top = mLayout.getLineTop(lineStart);
5125 // This is ridiculous, but the descent from the line above
5126 // can hang down into the line we really want to redraw,
5127 // so we have to invalidate part of the line above to make
5128 // sure everything that needs to be redrawn really is.
5129 // (But not the whole line above, because that would cause
5130 // the same problem with the descenders on the line above it!)
5131 if (lineStart > 0) {
5132 top -= mLayout.getLineDescent(lineStart - 1);
5138 lineEnd = lineStart;
5140 lineEnd = mLayout.getLineForOffset(end);
5142 int bottom = mLayout.getLineBottom(lineEnd);
5144 // mEditor can be null in case selection is set programmatically.
5145 if (invalidateCursor && mEditor != null) {
5146 for (int i = 0; i < mEditor.mCursorCount; i++) {
5147 Rect bounds = mEditor.mCursorDrawable[i].getBounds();
5148 top = Math.min(top, bounds.top);
5149 bottom = Math.max(bottom, bounds.bottom);
5153 final int compoundPaddingLeft = getCompoundPaddingLeft();
5154 final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true);
5157 if (lineStart == lineEnd && !invalidateCursor) {
5158 left = (int) mLayout.getPrimaryHorizontal(start);
5159 right = (int) (mLayout.getPrimaryHorizontal(end) + 1.0);
5160 left += compoundPaddingLeft;
5161 right += compoundPaddingLeft;
5163 // Rectangle bounding box when the region spans several lines
5164 left = compoundPaddingLeft;
5165 right = getWidth() - getCompoundPaddingRight();
5168 invalidate(mScrollX + left, verticalPadding + top,
5169 mScrollX + right, verticalPadding + bottom);
5173 private void registerForPreDraw() {
5174 if (!mPreDrawRegistered) {
5175 getViewTreeObserver().addOnPreDrawListener(this);
5176 mPreDrawRegistered = true;
5180 private void unregisterForPreDraw() {
5181 getViewTreeObserver().removeOnPreDrawListener(this);
5182 mPreDrawRegistered = false;
5183 mPreDrawListenerDetached = false;
5189 public boolean onPreDraw() {
5190 if (mLayout == null) {
5194 if (mMovement != null) {
5195 /* This code also provides auto-scrolling when a cursor is moved using a
5196 * CursorController (insertion point or selection limits).
5197 * For selection, ensure start or end is visible depending on controller's state.
5199 int curs = getSelectionEnd();
5200 // Do not create the controller if it is not already created.
5201 if (mEditor != null && mEditor.mSelectionModifierCursorController != null &&
5202 mEditor.mSelectionModifierCursorController.isSelectionStartDragged()) {
5203 curs = getSelectionStart();
5207 * TODO: This should really only keep the end in view if
5208 * it already was before the text changed. I'm not sure
5209 * of a good way to tell from here if it was.
5211 if (curs < 0 && (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
5212 curs = mText.length();
5216 bringPointIntoView(curs);
5219 bringTextIntoView();
5222 // This has to be checked here since:
5223 // - onFocusChanged cannot start it when focus is given to a view with selected text (after
5224 // a screen rotation) since layout is not yet initialized at that point.
5225 if (mEditor != null && mEditor.mCreatedWithASelection) {
5226 mEditor.startSelectionActionMode();
5227 mEditor.mCreatedWithASelection = false;
5230 // Phone specific code (there is no ExtractEditText on tablets).
5231 // ExtractEditText does not call onFocus when it is displayed, and mHasSelectionOnFocus can
5232 // not be set. Do the test here instead.
5233 if (isInExtractedMode() && hasSelection() && mEditor != null
5234 && mEditor.mTextActionMode == null) {
5235 mEditor.startSelectionActionMode();
5238 unregisterForPreDraw();
5244 protected void onAttachedToWindow() {
5245 super.onAttachedToWindow();
5247 mTemporaryDetach = false;
5249 if (mEditor != null) mEditor.onAttachedToWindow();
5251 if (mPreDrawListenerDetached) {
5252 getViewTreeObserver().addOnPreDrawListener(this);
5253 mPreDrawListenerDetached = false;
5259 protected void onDetachedFromWindowInternal() {
5260 if (mPreDrawRegistered) {
5261 getViewTreeObserver().removeOnPreDrawListener(this);
5262 mPreDrawListenerDetached = true;
5265 resetResolvedDrawables();
5267 if (mEditor != null) mEditor.onDetachedFromWindow();
5269 super.onDetachedFromWindowInternal();
5273 public void onScreenStateChanged(int screenState) {
5274 super.onScreenStateChanged(screenState);
5275 if (mEditor != null) mEditor.onScreenStateChanged(screenState);
5279 protected boolean isPaddingOffsetRequired() {
5280 return mShadowRadius != 0 || mDrawables != null;
5284 protected int getLeftPaddingOffset() {
5285 return getCompoundPaddingLeft() - mPaddingLeft +
5286 (int) Math.min(0, mShadowDx - mShadowRadius);
5290 protected int getTopPaddingOffset() {
5291 return (int) Math.min(0, mShadowDy - mShadowRadius);
5295 protected int getBottomPaddingOffset() {
5296 return (int) Math.max(0, mShadowDy + mShadowRadius);
5299 private int getFudgedPaddingRight() {
5300 // Add sufficient space for cursor and tone marks
5301 int cursorWidth = 2 + (int)mTextPaint.density; // adequate for Material cursors
5302 return Math.max(0, getCompoundPaddingRight() - (cursorWidth - 1));
5306 protected int getRightPaddingOffset() {
5307 return -(getFudgedPaddingRight() - mPaddingRight) +
5308 (int) Math.max(0, mShadowDx + mShadowRadius);
5312 protected boolean verifyDrawable(Drawable who) {
5313 final boolean verified = super.verifyDrawable(who);
5314 if (!verified && mDrawables != null) {
5315 for (Drawable dr : mDrawables.mShowing) {
5325 public void jumpDrawablesToCurrentState() {
5326 super.jumpDrawablesToCurrentState();
5327 if (mDrawables != null) {
5328 for (Drawable dr : mDrawables.mShowing) {
5330 dr.jumpToCurrentState();
5337 public void invalidateDrawable(Drawable drawable) {
5338 boolean handled = false;
5340 if (verifyDrawable(drawable)) {
5341 final Rect dirty = drawable.getBounds();
5342 int scrollX = mScrollX;
5343 int scrollY = mScrollY;
5345 // IMPORTANT: The coordinates below are based on the coordinates computed
5346 // for each compound drawable in onDraw(). Make sure to update each section
5348 final TextView.Drawables drawables = mDrawables;
5349 if (drawables != null) {
5350 if (drawable == drawables.mShowing[Drawables.LEFT]) {
5351 final int compoundPaddingTop = getCompoundPaddingTop();
5352 final int compoundPaddingBottom = getCompoundPaddingBottom();
5353 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
5355 scrollX += mPaddingLeft;
5356 scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2;
5358 } else if (drawable == drawables.mShowing[Drawables.RIGHT]) {
5359 final int compoundPaddingTop = getCompoundPaddingTop();
5360 final int compoundPaddingBottom = getCompoundPaddingBottom();
5361 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
5363 scrollX += (mRight - mLeft - mPaddingRight - drawables.mDrawableSizeRight);
5364 scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2;
5366 } else if (drawable == drawables.mShowing[Drawables.TOP]) {
5367 final int compoundPaddingLeft = getCompoundPaddingLeft();
5368 final int compoundPaddingRight = getCompoundPaddingRight();
5369 final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
5371 scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2;
5372 scrollY += mPaddingTop;
5374 } else if (drawable == drawables.mShowing[Drawables.BOTTOM]) {
5375 final int compoundPaddingLeft = getCompoundPaddingLeft();
5376 final int compoundPaddingRight = getCompoundPaddingRight();
5377 final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
5379 scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2;
5380 scrollY += (mBottom - mTop - mPaddingBottom - drawables.mDrawableSizeBottom);
5386 invalidate(dirty.left + scrollX, dirty.top + scrollY,
5387 dirty.right + scrollX, dirty.bottom + scrollY);
5392 super.invalidateDrawable(drawable);
5397 public boolean hasOverlappingRendering() {
5398 // horizontal fading edge causes SaveLayerAlpha, which doesn't support alpha modulation
5399 return ((getBackground() != null && getBackground().getCurrent() != null)
5400 || mText instanceof Spannable || hasSelection()
5401 || isHorizontalFadingEdgeEnabled());
5406 * Returns the state of the {@code textIsSelectable} flag (See
5407 * {@link #setTextIsSelectable setTextIsSelectable()}). Although you have to set this flag
5408 * to allow users to select and copy text in a non-editable TextView, the content of an
5409 * {@link EditText} can always be selected, independently of the value of this flag.
5412 * @return True if the text displayed in this TextView can be selected by the user.
5414 * @attr ref android.R.styleable#TextView_textIsSelectable
5416 public boolean isTextSelectable() {
5417 return mEditor == null ? false : mEditor.mTextIsSelectable;
5421 * Sets whether the content of this view is selectable by the user. The default is
5422 * {@code false}, meaning that the content is not selectable.
5424 * When you use a TextView to display a useful piece of information to the user (such as a
5425 * contact's address), make it selectable, so that the user can select and copy its
5426 * content. You can also use set the XML attribute
5427 * {@link android.R.styleable#TextView_textIsSelectable} to "true".
5429 * When you call this method to set the value of {@code textIsSelectable}, it sets
5430 * the flags {@code focusable}, {@code focusableInTouchMode}, {@code clickable},
5431 * and {@code longClickable} to the same value. These flags correspond to the attributes
5432 * {@link android.R.styleable#View_focusable android:focusable},
5433 * {@link android.R.styleable#View_focusableInTouchMode android:focusableInTouchMode},
5434 * {@link android.R.styleable#View_clickable android:clickable}, and
5435 * {@link android.R.styleable#View_longClickable android:longClickable}. To restore any of these
5436 * flags to a state you had set previously, call one or more of the following methods:
5437 * {@link #setFocusable(boolean) setFocusable()},
5438 * {@link #setFocusableInTouchMode(boolean) setFocusableInTouchMode()},
5439 * {@link #setClickable(boolean) setClickable()} or
5440 * {@link #setLongClickable(boolean) setLongClickable()}.
5442 * @param selectable Whether the content of this TextView should be selectable.
5444 public void setTextIsSelectable(boolean selectable) {
5445 if (!selectable && mEditor == null) return; // false is default value with no edit data
5447 createEditorIfNeeded();
5448 if (mEditor.mTextIsSelectable == selectable) return;
5450 mEditor.mTextIsSelectable = selectable;
5451 setFocusableInTouchMode(selectable);
5452 setFocusable(selectable);
5453 setClickable(selectable);
5454 setLongClickable(selectable);
5456 // mInputType should already be EditorInfo.TYPE_NULL and mInput should be null
5458 setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
5459 setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);
5461 // Called by setText above, but safer in case of future code changes
5462 mEditor.prepareCursorControllers();
5466 protected int[] onCreateDrawableState(int extraSpace) {
5467 final int[] drawableState;
5470 drawableState = super.onCreateDrawableState(extraSpace);
5472 drawableState = super.onCreateDrawableState(extraSpace + 1);
5473 mergeDrawableStates(drawableState, MULTILINE_STATE_SET);
5476 if (isTextSelectable()) {
5477 // Disable pressed state, which was introduced when TextView was made clickable.
5478 // Prevents text color change.
5479 // setClickable(false) would have a similar effect, but it also disables focus changes
5480 // and long press actions, which are both needed by text selection.
5481 final int length = drawableState.length;
5482 for (int i = 0; i < length; i++) {
5483 if (drawableState[i] == R.attr.state_pressed) {
5484 final int[] nonPressedState = new int[length - 1];
5485 System.arraycopy(drawableState, 0, nonPressedState, 0, i);
5486 System.arraycopy(drawableState, i + 1, nonPressedState, i, length - i - 1);
5487 return nonPressedState;
5492 return drawableState;
5495 private Path getUpdatedHighlightPath() {
5496 Path highlight = null;
5497 Paint highlightPaint = mHighlightPaint;
5499 final int selStart = getSelectionStart();
5500 final int selEnd = getSelectionEnd();
5501 if (mMovement != null && (isFocused() || isPressed()) && selStart >= 0) {
5502 if (selStart == selEnd) {
5503 if (mEditor != null && mEditor.isCursorVisible() &&
5504 (SystemClock.uptimeMillis() - mEditor.mShowCursor) %
5505 (2 * Editor.BLINK) < Editor.BLINK) {
5506 if (mHighlightPathBogus) {
5507 if (mHighlightPath == null) mHighlightPath = new Path();
5508 mHighlightPath.reset();
5509 mLayout.getCursorPath(selStart, mHighlightPath, mText);
5510 mEditor.updateCursorsPositions();
5511 mHighlightPathBogus = false;
5514 // XXX should pass to skin instead of drawing directly
5515 highlightPaint.setColor(mCurTextColor);
5516 highlightPaint.setStyle(Paint.Style.STROKE);
5517 highlight = mHighlightPath;
5520 if (mHighlightPathBogus) {
5521 if (mHighlightPath == null) mHighlightPath = new Path();
5522 mHighlightPath.reset();
5523 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath);
5524 mHighlightPathBogus = false;
5527 // XXX should pass to skin instead of drawing directly
5528 highlightPaint.setColor(mHighlightColor);
5529 highlightPaint.setStyle(Paint.Style.FILL);
5531 highlight = mHighlightPath;
5540 public int getHorizontalOffsetForDrawables() {
5545 protected void onDraw(Canvas canvas) {
5546 restartMarqueeIfNeeded();
5548 // Draw the background for this view
5549 super.onDraw(canvas);
5551 final int compoundPaddingLeft = getCompoundPaddingLeft();
5552 final int compoundPaddingTop = getCompoundPaddingTop();
5553 final int compoundPaddingRight = getCompoundPaddingRight();
5554 final int compoundPaddingBottom = getCompoundPaddingBottom();
5555 final int scrollX = mScrollX;
5556 final int scrollY = mScrollY;
5557 final int right = mRight;
5558 final int left = mLeft;
5559 final int bottom = mBottom;
5560 final int top = mTop;
5561 final boolean isLayoutRtl = isLayoutRtl();
5562 final int offset = getHorizontalOffsetForDrawables();
5563 final int leftOffset = isLayoutRtl ? 0 : offset;
5564 final int rightOffset = isLayoutRtl ? offset : 0 ;
5566 final Drawables dr = mDrawables;
5569 * Compound, not extended, because the icon is not clipped
5570 * if the text height is smaller.
5573 int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
5574 int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;
5576 // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
5577 // Make sure to update invalidateDrawable() when changing this code.
5578 if (dr.mShowing[Drawables.LEFT] != null) {
5580 canvas.translate(scrollX + mPaddingLeft + leftOffset,
5581 scrollY + compoundPaddingTop +
5582 (vspace - dr.mDrawableHeightLeft) / 2);
5583 dr.mShowing[Drawables.LEFT].draw(canvas);
5587 // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
5588 // Make sure to update invalidateDrawable() when changing this code.
5589 if (dr.mShowing[Drawables.RIGHT] != null) {
5591 canvas.translate(scrollX + right - left - mPaddingRight
5592 - dr.mDrawableSizeRight - rightOffset,
5593 scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2);
5594 dr.mShowing[Drawables.RIGHT].draw(canvas);
5598 // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
5599 // Make sure to update invalidateDrawable() when changing this code.
5600 if (dr.mShowing[Drawables.TOP] != null) {
5602 canvas.translate(scrollX + compoundPaddingLeft +
5603 (hspace - dr.mDrawableWidthTop) / 2, scrollY + mPaddingTop);
5604 dr.mShowing[Drawables.TOP].draw(canvas);
5608 // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
5609 // Make sure to update invalidateDrawable() when changing this code.
5610 if (dr.mShowing[Drawables.BOTTOM] != null) {
5612 canvas.translate(scrollX + compoundPaddingLeft +
5613 (hspace - dr.mDrawableWidthBottom) / 2,
5614 scrollY + bottom - top - mPaddingBottom - dr.mDrawableSizeBottom);
5615 dr.mShowing[Drawables.BOTTOM].draw(canvas);
5620 int color = mCurTextColor;
5622 if (mLayout == null) {
5626 Layout layout = mLayout;
5628 if (mHint != null && mText.length() == 0) {
5629 if (mHintTextColor != null) {
5630 color = mCurHintTextColor;
5633 layout = mHintLayout;
5636 mTextPaint.setColor(color);
5637 mTextPaint.drawableState = getDrawableState();
5640 /* Would be faster if we didn't have to do this. Can we chop the
5641 (displayable) text so that we don't need to do this ever?
5644 int extendedPaddingTop = getExtendedPaddingTop();
5645 int extendedPaddingBottom = getExtendedPaddingBottom();
5647 final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
5648 final int maxScrollY = mLayout.getHeight() - vspace;
5650 float clipLeft = compoundPaddingLeft + scrollX;
5651 float clipTop = (scrollY == 0) ? 0 : extendedPaddingTop + scrollY;
5652 float clipRight = right - left - getFudgedPaddingRight() + scrollX;
5653 float clipBottom = bottom - top + scrollY -
5654 ((scrollY == maxScrollY) ? 0 : extendedPaddingBottom);
5656 if (mShadowRadius != 0) {
5657 clipLeft += Math.min(0, mShadowDx - mShadowRadius);
5658 clipRight += Math.max(0, mShadowDx + mShadowRadius);
5660 clipTop += Math.min(0, mShadowDy - mShadowRadius);
5661 clipBottom += Math.max(0, mShadowDy + mShadowRadius);
5664 canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
5666 int voffsetText = 0;
5667 int voffsetCursor = 0;
5669 // translate in by our padding
5670 /* shortcircuit calling getVerticaOffset() */
5671 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
5672 voffsetText = getVerticalOffset(false);
5673 voffsetCursor = getVerticalOffset(true);
5675 canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText);
5677 final int layoutDirection = getLayoutDirection();
5678 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
5679 if (mEllipsize == TextUtils.TruncateAt.MARQUEE &&
5680 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
5681 if (!mSingleLine && getLineCount() == 1 && canMarquee() &&
5682 (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) {
5683 final int width = mRight - mLeft;
5684 final int padding = getCompoundPaddingLeft() + getCompoundPaddingRight();
5685 final float dx = mLayout.getLineRight(0) - (width - padding);
5686 canvas.translate(layout.getParagraphDirection(0) * dx, 0.0f);
5689 if (mMarquee != null && mMarquee.isRunning()) {
5690 final float dx = -mMarquee.getScroll();
5691 canvas.translate(layout.getParagraphDirection(0) * dx, 0.0f);
5695 final int cursorOffsetVertical = voffsetCursor - voffsetText;
5697 Path highlight = getUpdatedHighlightPath();
5698 if (mEditor != null) {
5699 mEditor.onDraw(canvas, layout, highlight, mHighlightPaint, cursorOffsetVertical);
5701 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical);
5704 if (mMarquee != null && mMarquee.shouldDrawGhost()) {
5705 final float dx = mMarquee.getGhostOffset();
5706 canvas.translate(layout.getParagraphDirection(0) * dx, 0.0f);
5707 layout.draw(canvas, highlight, mHighlightPaint, cursorOffsetVertical);
5714 public void getFocusedRect(Rect r) {
5715 if (mLayout == null) {
5716 super.getFocusedRect(r);
5720 int selEnd = getSelectionEnd();
5722 super.getFocusedRect(r);
5726 int selStart = getSelectionStart();
5727 if (selStart < 0 || selStart >= selEnd) {
5728 int line = mLayout.getLineForOffset(selEnd);
5729 r.top = mLayout.getLineTop(line);
5730 r.bottom = mLayout.getLineBottom(line);
5731 r.left = (int) mLayout.getPrimaryHorizontal(selEnd) - 2;
5732 r.right = r.left + 4;
5734 int lineStart = mLayout.getLineForOffset(selStart);
5735 int lineEnd = mLayout.getLineForOffset(selEnd);
5736 r.top = mLayout.getLineTop(lineStart);
5737 r.bottom = mLayout.getLineBottom(lineEnd);
5738 if (lineStart == lineEnd) {
5739 r.left = (int) mLayout.getPrimaryHorizontal(selStart);
5740 r.right = (int) mLayout.getPrimaryHorizontal(selEnd);
5742 // Selection extends across multiple lines -- make the focused
5743 // rect cover the entire width.
5744 if (mHighlightPathBogus) {
5745 if (mHighlightPath == null) mHighlightPath = new Path();
5746 mHighlightPath.reset();
5747 mLayout.getSelectionPath(selStart, selEnd, mHighlightPath);
5748 mHighlightPathBogus = false;
5750 synchronized (TEMP_RECTF) {
5751 mHighlightPath.computeBounds(TEMP_RECTF, true);
5752 r.left = (int)TEMP_RECTF.left-1;
5753 r.right = (int)TEMP_RECTF.right+1;
5758 // Adjust for padding and gravity.
5759 int paddingLeft = getCompoundPaddingLeft();
5760 int paddingTop = getExtendedPaddingTop();
5761 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
5762 paddingTop += getVerticalOffset(false);
5764 r.offset(paddingLeft, paddingTop);
5765 int paddingBottom = getExtendedPaddingBottom();
5766 r.bottom += paddingBottom;
5770 * Return the number of lines of text, or 0 if the internal Layout has not
5773 public int getLineCount() {
5774 return mLayout != null ? mLayout.getLineCount() : 0;
5778 * Return the baseline for the specified line (0...getLineCount() - 1)
5779 * If bounds is not null, return the top, left, right, bottom extents
5780 * of the specified line in it. If the internal Layout has not been built,
5781 * return 0 and set bounds to (0, 0, 0, 0)
5782 * @param line which line to examine (0..getLineCount() - 1)
5783 * @param bounds Optional. If not null, it returns the extent of the line
5784 * @return the Y-coordinate of the baseline
5786 public int getLineBounds(int line, Rect bounds) {
5787 if (mLayout == null) {
5788 if (bounds != null) {
5789 bounds.set(0, 0, 0, 0);
5794 int baseline = mLayout.getLineBounds(line, bounds);
5796 int voffset = getExtendedPaddingTop();
5797 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
5798 voffset += getVerticalOffset(true);
5800 if (bounds != null) {
5801 bounds.offset(getCompoundPaddingLeft(), voffset);
5803 return baseline + voffset;
5808 public int getBaseline() {
5809 if (mLayout == null) {
5810 return super.getBaseline();
5814 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
5815 voffset = getVerticalOffset(true);
5818 if (isLayoutModeOptical(mParent)) {
5819 voffset -= getOpticalInsets().top;
5822 return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0);
5829 protected int getFadeTop(boolean offsetRequired) {
5830 if (mLayout == null) return 0;
5833 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
5834 voffset = getVerticalOffset(true);
5837 if (offsetRequired) voffset += getTopPaddingOffset();
5839 return getExtendedPaddingTop() + voffset;
5846 protected int getFadeHeight(boolean offsetRequired) {
5847 return mLayout != null ? mLayout.getHeight() : 0;
5851 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5852 // Note: If the IME is in fullscreen mode and IMS#mExtractEditText is in text action mode,
5853 // InputMethodService#onKeyDown and InputMethodService#onKeyUp are responsible to call
5854 // InputMethodService#mExtractEditText.maybeHandleBackInTextActionMode(event).
5855 if (keyCode == KeyEvent.KEYCODE_BACK && handleBackInTextActionModeIfNeeded(event)) {
5858 return super.onKeyPreIme(keyCode, event);
5864 public boolean handleBackInTextActionModeIfNeeded(KeyEvent event) {
5865 // Do nothing unless mEditor is in text action mode.
5866 if (mEditor == null || mEditor.mTextActionMode == null) {
5870 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
5871 KeyEvent.DispatcherState state = getKeyDispatcherState();
5872 if (state != null) {
5873 state.startTracking(event, this);
5876 } else if (event.getAction() == KeyEvent.ACTION_UP) {
5877 KeyEvent.DispatcherState state = getKeyDispatcherState();
5878 if (state != null) {
5879 state.handleUpEvent(event);
5881 if (event.isTracking() && !event.isCanceled()) {
5882 stopTextActionMode();
5890 public boolean onKeyDown(int keyCode, KeyEvent event) {
5891 int which = doKeyDown(keyCode, event, null);
5893 return super.onKeyDown(keyCode, event);
5900 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5901 KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN);
5903 int which = doKeyDown(keyCode, down, event);
5905 // Go through default dispatching.
5906 return super.onKeyMultiple(keyCode, repeatCount, event);
5909 // Consumed the whole thing.
5915 // We are going to dispatch the remaining events to either the input
5916 // or movement method. To do this, we will just send a repeated stream
5917 // of down and up events until we have done the complete repeatCount.
5918 // It would be nice if those interfaces had an onKeyMultiple() method,
5919 // but adding that is a more complicated change.
5920 KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
5922 // mEditor and mEditor.mInput are not null from doKeyDown
5923 mEditor.mKeyListener.onKeyUp(this, (Editable)mText, keyCode, up);
5924 while (--repeatCount > 0) {
5925 mEditor.mKeyListener.onKeyDown(this, (Editable)mText, keyCode, down);
5926 mEditor.mKeyListener.onKeyUp(this, (Editable)mText, keyCode, up);
5928 hideErrorIfUnchanged();
5930 } else if (which == 2) {
5931 // mMovement is not null from doKeyDown
5932 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up);
5933 while (--repeatCount > 0) {
5934 mMovement.onKeyDown(this, (Spannable)mText, keyCode, down);
5935 mMovement.onKeyUp(this, (Spannable)mText, keyCode, up);
5943 * Returns true if pressing ENTER in this field advances focus instead
5944 * of inserting the character. This is true mostly in single-line fields,
5945 * but also in mail addresses and subjects which will display on multiple
5946 * lines but where it doesn't make sense to insert newlines.
5948 private boolean shouldAdvanceFocusOnEnter() {
5949 if (getKeyListener() == null) {
5957 if (mEditor != null &&
5958 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
5959 int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION;
5960 if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
5961 || variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT) {
5970 * Returns true if pressing TAB in this field advances focus instead
5971 * of inserting the character. Insert tabs only in multi-line editors.
5973 private boolean shouldAdvanceFocusOnTab() {
5974 if (getKeyListener() != null && !mSingleLine && mEditor != null &&
5975 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
5976 int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION;
5977 if (variation == EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE
5978 || variation == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) {
5985 private int doKeyDown(int keyCode, KeyEvent event, KeyEvent otherEvent) {
5990 // If this is the initial keydown, we don't want to prevent a movement away from this view.
5991 // While this shouldn't be necessary because any time we're preventing default movement we
5992 // should be restricting the focus to remain within this view, thus we'll also receive
5993 // the key up event, occasionally key up events will get dropped and we don't want to
5994 // prevent the user from traversing out of this on the next key down.
5995 if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) {
5996 mPreventDefaultMovement = false;
6000 case KeyEvent.KEYCODE_ENTER:
6001 if (event.hasNoModifiers()) {
6002 // When mInputContentType is set, we know that we are
6003 // running in a "modern" cupcake environment, so don't need
6004 // to worry about the application trying to capture
6005 // enter key events.
6006 if (mEditor != null && mEditor.mInputContentType != null) {
6007 // If there is an action listener, given them a
6008 // chance to consume the event.
6009 if (mEditor.mInputContentType.onEditorActionListener != null &&
6010 mEditor.mInputContentType.onEditorActionListener.onEditorAction(
6011 this, EditorInfo.IME_NULL, event)) {
6012 mEditor.mInputContentType.enterDown = true;
6013 // We are consuming the enter key for them.
6018 // If our editor should move focus when enter is pressed, or
6019 // this is a generated event from an IME action button, then
6020 // don't let it be inserted into the text.
6021 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0
6022 || shouldAdvanceFocusOnEnter()) {
6023 if (hasOnClickListeners()) {
6031 case KeyEvent.KEYCODE_DPAD_CENTER:
6032 if (event.hasNoModifiers()) {
6033 if (shouldAdvanceFocusOnEnter()) {
6039 case KeyEvent.KEYCODE_TAB:
6040 if (event.hasNoModifiers() || event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
6041 if (shouldAdvanceFocusOnTab()) {
6047 // Has to be done on key down (and not on key up) to correctly be intercepted.
6048 case KeyEvent.KEYCODE_BACK:
6049 if (mEditor != null && mEditor.mTextActionMode != null) {
6050 stopTextActionMode();
6056 if (mEditor != null && mEditor.mKeyListener != null) {
6057 boolean doDown = true;
6058 if (otherEvent != null) {
6061 final boolean handled = mEditor.mKeyListener.onKeyOther(this, (Editable) mText,
6063 hideErrorIfUnchanged();
6068 } catch (AbstractMethodError e) {
6069 // onKeyOther was added after 1.0, so if it isn't
6070 // implemented we need to try to dispatch as a regular down.
6078 final boolean handled = mEditor.mKeyListener.onKeyDown(this, (Editable) mText,
6081 hideErrorIfUnchanged();
6082 if (handled) return 1;
6086 // bug 650865: sometimes we get a key event before a layout.
6087 // don't try to move around if we don't know the layout.
6089 if (mMovement != null && mLayout != null) {
6090 boolean doDown = true;
6091 if (otherEvent != null) {
6093 boolean handled = mMovement.onKeyOther(this, (Spannable) mText,
6099 } catch (AbstractMethodError e) {
6100 // onKeyOther was added after 1.0, so if it isn't
6101 // implemented we need to try to dispatch as a regular down.
6105 if (mMovement.onKeyDown(this, (Spannable)mText, keyCode, event)) {
6106 if (event.getRepeatCount() == 0 && !KeyEvent.isModifierKey(keyCode)) {
6107 mPreventDefaultMovement = true;
6114 return mPreventDefaultMovement && !KeyEvent.isModifierKey(keyCode) ? -1 : 0;
6118 * Resets the mErrorWasChanged flag, so that future calls to {@link #setError(CharSequence)}
6122 public void resetErrorChangedFlag() {
6124 * Keep track of what the error was before doing the input
6125 * so that if an input filter changed the error, we leave
6126 * that error showing. Otherwise, we take down whatever
6127 * error was showing when the user types something.
6129 if (mEditor != null) mEditor.mErrorWasChanged = false;
6135 public void hideErrorIfUnchanged() {
6136 if (mEditor != null && mEditor.mError != null && !mEditor.mErrorWasChanged) {
6137 setError(null, null);
6142 public boolean onKeyUp(int keyCode, KeyEvent event) {
6144 return super.onKeyUp(keyCode, event);
6147 if (!KeyEvent.isModifierKey(keyCode)) {
6148 mPreventDefaultMovement = false;
6152 case KeyEvent.KEYCODE_DPAD_CENTER:
6153 if (event.hasNoModifiers()) {
6155 * If there is a click listener, just call through to
6156 * super, which will invoke it.
6158 * If there isn't a click listener, try to show the soft
6159 * input method. (It will also
6160 * call performClick(), but that won't do anything in
6163 if (!hasOnClickListeners()) {
6164 if (mMovement != null && mText instanceof Editable
6165 && mLayout != null && onCheckIsTextEditor()) {
6166 InputMethodManager imm = InputMethodManager.peekInstance();
6168 if (imm != null && getShowSoftInputOnFocus()) {
6169 imm.showSoftInput(this, 0);
6174 return super.onKeyUp(keyCode, event);
6176 case KeyEvent.KEYCODE_ENTER:
6177 if (event.hasNoModifiers()) {
6178 if (mEditor != null && mEditor.mInputContentType != null
6179 && mEditor.mInputContentType.onEditorActionListener != null
6180 && mEditor.mInputContentType.enterDown) {
6181 mEditor.mInputContentType.enterDown = false;
6182 if (mEditor.mInputContentType.onEditorActionListener.onEditorAction(
6183 this, EditorInfo.IME_NULL, event)) {
6188 if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0
6189 || shouldAdvanceFocusOnEnter()) {
6191 * If there is a click listener, just call through to
6192 * super, which will invoke it.
6194 * If there isn't a click listener, try to advance focus,
6195 * but still call through to super, which will reset the
6196 * pressed state and longpress state. (It will also
6197 * call performClick(), but that won't do anything in
6200 if (!hasOnClickListeners()) {
6201 View v = focusSearch(FOCUS_DOWN);
6204 if (!v.requestFocus(FOCUS_DOWN)) {
6205 throw new IllegalStateException(
6206 "focus search returned a view " +
6207 "that wasn't able to take focus!");
6211 * Return true because we handled the key; super
6212 * will return false because there was no click
6215 super.onKeyUp(keyCode, event);
6217 } else if ((event.getFlags()
6218 & KeyEvent.FLAG_EDITOR_ACTION) != 0) {
6219 // No target for next focus, but make sure the IME
6220 // if this came from it.
6221 InputMethodManager imm = InputMethodManager.peekInstance();
6222 if (imm != null && imm.isActive(this)) {
6223 imm.hideSoftInputFromWindow(getWindowToken(), 0);
6228 return super.onKeyUp(keyCode, event);
6233 if (mEditor != null && mEditor.mKeyListener != null)
6234 if (mEditor.mKeyListener.onKeyUp(this, (Editable) mText, keyCode, event))
6237 if (mMovement != null && mLayout != null)
6238 if (mMovement.onKeyUp(this, (Spannable) mText, keyCode, event))
6241 return super.onKeyUp(keyCode, event);
6245 public boolean onCheckIsTextEditor() {
6246 return mEditor != null && mEditor.mInputType != EditorInfo.TYPE_NULL;
6250 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6251 if (onCheckIsTextEditor() && isEnabled()) {
6252 mEditor.createInputMethodStateIfNeeded();
6253 outAttrs.inputType = getInputType();
6254 if (mEditor.mInputContentType != null) {
6255 outAttrs.imeOptions = mEditor.mInputContentType.imeOptions;
6256 outAttrs.privateImeOptions = mEditor.mInputContentType.privateImeOptions;
6257 outAttrs.actionLabel = mEditor.mInputContentType.imeActionLabel;
6258 outAttrs.actionId = mEditor.mInputContentType.imeActionId;
6259 outAttrs.extras = mEditor.mInputContentType.extras;
6261 outAttrs.imeOptions = EditorInfo.IME_NULL;
6263 if (focusSearch(FOCUS_DOWN) != null) {
6264 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT;
6266 if (focusSearch(FOCUS_UP) != null) {
6267 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS;
6269 if ((outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION)
6270 == EditorInfo.IME_ACTION_UNSPECIFIED) {
6271 if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) {
6272 // An action has not been set, but the enter key will move to
6273 // the next focus, so set the action to that.
6274 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
6276 // An action has not been set, and there is no focus to move
6277 // to, so let's just supply a "done" action.
6278 outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
6280 if (!shouldAdvanceFocusOnEnter()) {
6281 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION;
6284 if (isMultilineInputType(outAttrs.inputType)) {
6285 // Multi-line text editors should always show an enter key.
6286 outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_ENTER_ACTION;
6288 outAttrs.hintText = mHint;
6289 if (mText instanceof Editable) {
6290 InputConnection ic = new EditableInputConnection(this);
6291 outAttrs.initialSelStart = getSelectionStart();
6292 outAttrs.initialSelEnd = getSelectionEnd();
6293 outAttrs.initialCapsMode = ic.getCursorCapsMode(getInputType());
6301 * If this TextView contains editable content, extract a portion of it
6302 * based on the information in <var>request</var> in to <var>outText</var>.
6303 * @return Returns true if the text was successfully extracted, else false.
6305 public boolean extractText(ExtractedTextRequest request, ExtractedText outText) {
6306 createEditorIfNeeded();
6307 return mEditor.extractText(request, outText);
6311 * This is used to remove all style-impacting spans from text before new
6312 * extracted text is being replaced into it, so that we don't have any
6313 * lingering spans applied during the replace.
6315 static void removeParcelableSpans(Spannable spannable, int start, int end) {
6316 Object[] spans = spannable.getSpans(start, end, ParcelableSpan.class);
6317 int i = spans.length;
6320 spannable.removeSpan(spans[i]);
6325 * Apply to this text view the given extracted text, as previously
6326 * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}.
6328 public void setExtractedText(ExtractedText text) {
6329 Editable content = getEditableText();
6330 if (text.text != null) {
6331 if (content == null) {
6332 setText(text.text, TextView.BufferType.EDITABLE);
6333 } else if (text.partialStartOffset < 0) {
6334 removeParcelableSpans(content, 0, content.length());
6335 content.replace(0, content.length(), text.text);
6337 final int N = content.length();
6338 int start = text.partialStartOffset;
6339 if (start > N) start = N;
6340 int end = text.partialEndOffset;
6341 if (end > N) end = N;
6342 removeParcelableSpans(content, start, end);
6343 content.replace(start, end, text.text);
6347 // Now set the selection position... make sure it is in range, to
6348 // avoid crashes. If this is a partial update, it is possible that
6349 // the underlying text may have changed, causing us problems here.
6350 // Also we just don't want to trust clients to do the right thing.
6351 Spannable sp = (Spannable)getText();
6352 final int N = sp.length();
6353 int start = text.selectionStart;
6354 if (start < 0) start = 0;
6355 else if (start > N) start = N;
6356 int end = text.selectionEnd;
6357 if (end < 0) end = 0;
6358 else if (end > N) end = N;
6359 Selection.setSelection(sp, start, end);
6361 // Finally, update the selection mode.
6362 if ((text.flags&ExtractedText.FLAG_SELECTING) != 0) {
6363 MetaKeyKeyListener.startSelecting(this, sp);
6365 MetaKeyKeyListener.stopSelecting(this, sp);
6372 public void setExtracting(ExtractedTextRequest req) {
6373 if (mEditor.mInputMethodState != null) {
6374 mEditor.mInputMethodState.mExtractedTextRequest = req;
6376 // This would stop a possible selection mode, but no such mode is started in case
6377 // extracted mode will start. Some text is selected though, and will trigger an action mode
6378 // in the extracted view.
6379 mEditor.hideCursorAndSpanControllers();
6380 stopTextActionMode();
6384 * Called by the framework in response to a text completion from
6385 * the current input method, provided by it calling
6386 * {@link InputConnection#commitCompletion
6387 * InputConnection.commitCompletion()}. The default implementation does
6388 * nothing; text views that are supporting auto-completion should override
6389 * this to do their desired behavior.
6391 * @param text The auto complete text the user has selected.
6393 public void onCommitCompletion(CompletionInfo text) {
6394 // intentionally empty
6398 * Called by the framework in response to a text auto-correction (such as fixing a typo using a
6399 * a dictionnary) from the current input method, provided by it calling
6400 * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default
6401 * implementation flashes the background of the corrected word to provide feedback to the user.
6403 * @param info The auto correct info about the text that was corrected.
6405 public void onCommitCorrection(CorrectionInfo info) {
6406 if (mEditor != null) mEditor.onCommitCorrection(info);
6409 public void beginBatchEdit() {
6410 if (mEditor != null) mEditor.beginBatchEdit();
6413 public void endBatchEdit() {
6414 if (mEditor != null) mEditor.endBatchEdit();
6418 * Called by the framework in response to a request to begin a batch
6419 * of edit operations through a call to link {@link #beginBatchEdit()}.
6421 public void onBeginBatchEdit() {
6422 // intentionally empty
6426 * Called by the framework in response to a request to end a batch
6427 * of edit operations through a call to link {@link #endBatchEdit}.
6429 public void onEndBatchEdit() {
6430 // intentionally empty
6434 * Called by the framework in response to a private command from the
6435 * current method, provided by it calling
6436 * {@link InputConnection#performPrivateCommand
6437 * InputConnection.performPrivateCommand()}.
6439 * @param action The action name of the command.
6440 * @param data Any additional data for the command. This may be null.
6441 * @return Return true if you handled the command, else false.
6443 public boolean onPrivateIMECommand(String action, Bundle data) {
6447 private void nullLayouts() {
6448 if (mLayout instanceof BoringLayout && mSavedLayout == null) {
6449 mSavedLayout = (BoringLayout) mLayout;
6451 if (mHintLayout instanceof BoringLayout && mSavedHintLayout == null) {
6452 mSavedHintLayout = (BoringLayout) mHintLayout;
6455 mSavedMarqueeModeLayout = mLayout = mHintLayout = null;
6457 mBoring = mHintBoring = null;
6459 // Since it depends on the value of mLayout
6460 if (mEditor != null) mEditor.prepareCursorControllers();
6464 * Make a new Layout based on the already-measured size of the view,
6465 * on the assumption that it was measured correctly at some point.
6467 private void assumeLayout() {
6468 int width = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
6474 int physicalWidth = width;
6476 if (mHorizontallyScrolling) {
6480 makeNewLayout(width, physicalWidth, UNKNOWN_BORING, UNKNOWN_BORING,
6481 physicalWidth, false);
6484 private Layout.Alignment getLayoutAlignment() {
6485 Layout.Alignment alignment;
6486 switch (getTextAlignment()) {
6487 case TEXT_ALIGNMENT_GRAVITY:
6488 switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
6490 alignment = Layout.Alignment.ALIGN_NORMAL;
6493 alignment = Layout.Alignment.ALIGN_OPPOSITE;
6496 alignment = Layout.Alignment.ALIGN_LEFT;
6499 alignment = Layout.Alignment.ALIGN_RIGHT;
6501 case Gravity.CENTER_HORIZONTAL:
6502 alignment = Layout.Alignment.ALIGN_CENTER;
6505 alignment = Layout.Alignment.ALIGN_NORMAL;
6509 case TEXT_ALIGNMENT_TEXT_START:
6510 alignment = Layout.Alignment.ALIGN_NORMAL;
6512 case TEXT_ALIGNMENT_TEXT_END:
6513 alignment = Layout.Alignment.ALIGN_OPPOSITE;
6515 case TEXT_ALIGNMENT_CENTER:
6516 alignment = Layout.Alignment.ALIGN_CENTER;
6518 case TEXT_ALIGNMENT_VIEW_START:
6519 alignment = (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
6520 Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT;
6522 case TEXT_ALIGNMENT_VIEW_END:
6523 alignment = (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
6524 Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT;
6526 case TEXT_ALIGNMENT_INHERIT:
6527 // This should never happen as we have already resolved the text alignment
6528 // but better safe than sorry so we just fall through
6530 alignment = Layout.Alignment.ALIGN_NORMAL;
6537 * The width passed in is now the desired layout width,
6538 * not the full view width with padding.
6541 protected void makeNewLayout(int wantWidth, int hintWidth,
6542 BoringLayout.Metrics boring,
6543 BoringLayout.Metrics hintBoring,
6544 int ellipsisWidth, boolean bringIntoView) {
6547 // Update "old" cached values
6548 mOldMaximum = mMaximum;
6549 mOldMaxMode = mMaxMode;
6551 mHighlightPathBogus = true;
6553 if (wantWidth < 0) {
6556 if (hintWidth < 0) {
6560 Layout.Alignment alignment = getLayoutAlignment();
6561 final boolean testDirChange = mSingleLine && mLayout != null &&
6562 (alignment == Layout.Alignment.ALIGN_NORMAL ||
6563 alignment == Layout.Alignment.ALIGN_OPPOSITE);
6565 if (testDirChange) oldDir = mLayout.getParagraphDirection(0);
6566 boolean shouldEllipsize = mEllipsize != null && getKeyListener() == null;
6567 final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE &&
6568 mMarqueeFadeMode != MARQUEE_FADE_NORMAL;
6569 TruncateAt effectiveEllipsize = mEllipsize;
6570 if (mEllipsize == TruncateAt.MARQUEE &&
6571 mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
6572 effectiveEllipsize = TruncateAt.END_SMALL;
6575 if (mTextDir == null) {
6576 mTextDir = getTextDirectionHeuristic();
6579 mLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, shouldEllipsize,
6580 effectiveEllipsize, effectiveEllipsize == mEllipsize);
6581 if (switchEllipsize) {
6582 TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ?
6583 TruncateAt.END : TruncateAt.MARQUEE;
6584 mSavedMarqueeModeLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment,
6585 shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize);
6588 shouldEllipsize = mEllipsize != null;
6591 if (mHint != null) {
6592 if (shouldEllipsize) hintWidth = wantWidth;
6594 if (hintBoring == UNKNOWN_BORING) {
6595 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir,
6597 if (hintBoring != null) {
6598 mHintBoring = hintBoring;
6602 if (hintBoring != null) {
6603 if (hintBoring.width <= hintWidth &&
6604 (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) {
6605 if (mSavedHintLayout != null) {
6606 mHintLayout = mSavedHintLayout.
6607 replaceOrMake(mHint, mTextPaint,
6608 hintWidth, alignment, mSpacingMult, mSpacingAdd,
6609 hintBoring, mIncludePad);
6611 mHintLayout = BoringLayout.make(mHint, mTextPaint,
6612 hintWidth, alignment, mSpacingMult, mSpacingAdd,
6613 hintBoring, mIncludePad);
6616 mSavedHintLayout = (BoringLayout) mHintLayout;
6617 } else if (shouldEllipsize && hintBoring.width <= hintWidth) {
6618 if (mSavedHintLayout != null) {
6619 mHintLayout = mSavedHintLayout.
6620 replaceOrMake(mHint, mTextPaint,
6621 hintWidth, alignment, mSpacingMult, mSpacingAdd,
6622 hintBoring, mIncludePad, mEllipsize,
6625 mHintLayout = BoringLayout.make(mHint, mTextPaint,
6626 hintWidth, alignment, mSpacingMult, mSpacingAdd,
6627 hintBoring, mIncludePad, mEllipsize,
6632 // TODO: code duplication with makeSingleLayout()
6633 if (mHintLayout == null) {
6634 StaticLayout.Builder builder = StaticLayout.Builder.obtain(mHint, 0,
6635 mHint.length(), mTextPaint, hintWidth)
6636 .setAlignment(alignment)
6637 .setTextDirection(mTextDir)
6638 .setLineSpacing(mSpacingAdd, mSpacingMult)
6639 .setIncludePad(mIncludePad)
6640 .setBreakStrategy(mBreakStrategy)
6641 .setHyphenationFrequency(mHyphenationFrequency);
6642 if (shouldEllipsize) {
6643 builder.setEllipsize(mEllipsize)
6644 .setEllipsizedWidth(ellipsisWidth)
6645 .setMaxLines(mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE);
6647 mHintLayout = builder.build();
6651 if (bringIntoView || (testDirChange && oldDir != mLayout.getParagraphDirection(0))) {
6652 registerForPreDraw();
6655 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
6656 if (!compressText(ellipsisWidth)) {
6657 final int height = mLayoutParams.height;
6658 // If the size of the view does not depend on the size of the text, try to
6659 // start the marquee immediately
6660 if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) {
6663 // Defer the start of the marquee until we know our width (see setFrame())
6664 mRestartMarquee = true;
6669 // CursorControllers need a non-null mLayout
6670 if (mEditor != null) mEditor.prepareCursorControllers();
6673 private Layout makeSingleLayout(int wantWidth, BoringLayout.Metrics boring, int ellipsisWidth,
6674 Layout.Alignment alignment, boolean shouldEllipsize, TruncateAt effectiveEllipsize,
6676 Layout result = null;
6677 if (mText instanceof Spannable) {
6678 result = new DynamicLayout(mText, mTransformed, mTextPaint, wantWidth,
6679 alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad,
6680 mBreakStrategy, mHyphenationFrequency,
6681 getKeyListener() == null ? effectiveEllipsize : null, ellipsisWidth);
6683 if (boring == UNKNOWN_BORING) {
6684 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring);
6685 if (boring != null) {
6690 if (boring != null) {
6691 if (boring.width <= wantWidth &&
6692 (effectiveEllipsize == null || boring.width <= ellipsisWidth)) {
6693 if (useSaved && mSavedLayout != null) {
6694 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint,
6695 wantWidth, alignment, mSpacingMult, mSpacingAdd,
6696 boring, mIncludePad);
6698 result = BoringLayout.make(mTransformed, mTextPaint,
6699 wantWidth, alignment, mSpacingMult, mSpacingAdd,
6700 boring, mIncludePad);
6704 mSavedLayout = (BoringLayout) result;
6706 } else if (shouldEllipsize && boring.width <= wantWidth) {
6707 if (useSaved && mSavedLayout != null) {
6708 result = mSavedLayout.replaceOrMake(mTransformed, mTextPaint,
6709 wantWidth, alignment, mSpacingMult, mSpacingAdd,
6710 boring, mIncludePad, effectiveEllipsize,
6713 result = BoringLayout.make(mTransformed, mTextPaint,
6714 wantWidth, alignment, mSpacingMult, mSpacingAdd,
6715 boring, mIncludePad, effectiveEllipsize,
6721 if (result == null) {
6722 StaticLayout.Builder builder = StaticLayout.Builder.obtain(mTransformed,
6723 0, mTransformed.length(), mTextPaint, wantWidth)
6724 .setAlignment(alignment)
6725 .setTextDirection(mTextDir)
6726 .setLineSpacing(mSpacingAdd, mSpacingMult)
6727 .setIncludePad(mIncludePad)
6728 .setBreakStrategy(mBreakStrategy)
6729 .setHyphenationFrequency(mHyphenationFrequency);
6730 if (shouldEllipsize) {
6731 builder.setEllipsize(effectiveEllipsize)
6732 .setEllipsizedWidth(ellipsisWidth)
6733 .setMaxLines(mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE);
6735 // TODO: explore always setting maxLines
6736 result = builder.build();
6741 private boolean compressText(float width) {
6742 if (isHardwareAccelerated()) return false;
6744 // Only compress the text if it hasn't been compressed by the previous pass
6745 if (width > 0.0f && mLayout != null && getLineCount() == 1 && !mUserSetTextScaleX &&
6746 mTextPaint.getTextScaleX() == 1.0f) {
6747 final float textWidth = mLayout.getLineWidth(0);
6748 final float overflow = (textWidth + 1.0f - width) / width;
6749 if (overflow > 0.0f && overflow <= Marquee.MARQUEE_DELTA_MAX) {
6750 mTextPaint.setTextScaleX(1.0f - overflow - 0.005f);
6751 post(new Runnable() {
6763 private static int desired(Layout layout) {
6764 int n = layout.getLineCount();
6765 CharSequence text = layout.getText();
6768 // if any line was wrapped, we can't use it.
6769 // but it's ok for the last line not to have a newline
6771 for (int i = 0; i < n - 1; i++) {
6772 if (text.charAt(layout.getLineEnd(i) - 1) != '\n')
6776 for (int i = 0; i < n; i++) {
6777 max = Math.max(max, layout.getLineWidth(i));
6780 return (int) Math.ceil(max);
6784 * Set whether the TextView includes extra top and bottom padding to make
6785 * room for accents that go above the normal ascent and descent.
6786 * The default is true.
6788 * @see #getIncludeFontPadding()
6790 * @attr ref android.R.styleable#TextView_includeFontPadding
6792 public void setIncludeFontPadding(boolean includepad) {
6793 if (mIncludePad != includepad) {
6794 mIncludePad = includepad;
6796 if (mLayout != null) {
6805 * Gets whether the TextView includes extra top and bottom padding to make
6806 * room for accents that go above the normal ascent and descent.
6808 * @see #setIncludeFontPadding(boolean)
6810 * @attr ref android.R.styleable#TextView_includeFontPadding
6812 public boolean getIncludeFontPadding() {
6816 private static final BoringLayout.Metrics UNKNOWN_BORING = new BoringLayout.Metrics();
6819 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
6820 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
6821 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
6822 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
6823 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
6828 BoringLayout.Metrics boring = UNKNOWN_BORING;
6829 BoringLayout.Metrics hintBoring = UNKNOWN_BORING;
6831 if (mTextDir == null) {
6832 mTextDir = getTextDirectionHeuristic();
6836 boolean fromexisting = false;
6838 if (widthMode == MeasureSpec.EXACTLY) {
6839 // Parent has told us how big to be. So be it.
6842 if (mLayout != null && mEllipsize == null) {
6843 des = desired(mLayout);
6847 boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring);
6848 if (boring != null) {
6852 fromexisting = true;
6855 if (boring == null || boring == UNKNOWN_BORING) {
6857 des = (int) Math.ceil(Layout.getDesiredWidth(mTransformed, mTextPaint));
6861 width = boring.width;
6864 final Drawables dr = mDrawables;
6866 width = Math.max(width, dr.mDrawableWidthTop);
6867 width = Math.max(width, dr.mDrawableWidthBottom);
6870 if (mHint != null) {
6874 if (mHintLayout != null && mEllipsize == null) {
6875 hintDes = desired(mHintLayout);
6879 hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, mHintBoring);
6880 if (hintBoring != null) {
6881 mHintBoring = hintBoring;
6885 if (hintBoring == null || hintBoring == UNKNOWN_BORING) {
6887 hintDes = (int) Math.ceil(Layout.getDesiredWidth(mHint, mTextPaint));
6889 hintWidth = hintDes;
6891 hintWidth = hintBoring.width;
6894 if (hintWidth > width) {
6899 width += getCompoundPaddingLeft() + getCompoundPaddingRight();
6901 if (mMaxWidthMode == EMS) {
6902 width = Math.min(width, mMaxWidth * getLineHeight());
6904 width = Math.min(width, mMaxWidth);
6907 if (mMinWidthMode == EMS) {
6908 width = Math.max(width, mMinWidth * getLineHeight());
6910 width = Math.max(width, mMinWidth);
6913 // Check against our minimum width
6914 width = Math.max(width, getSuggestedMinimumWidth());
6916 if (widthMode == MeasureSpec.AT_MOST) {
6917 width = Math.min(widthSize, width);
6921 int want = width - getCompoundPaddingLeft() - getCompoundPaddingRight();
6922 int unpaddedWidth = want;
6924 if (mHorizontallyScrolling) want = VERY_WIDE;
6926 int hintWant = want;
6927 int hintWidth = (mHintLayout == null) ? hintWant : mHintLayout.getWidth();
6929 if (mLayout == null) {
6930 makeNewLayout(want, hintWant, boring, hintBoring,
6931 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false);
6933 final boolean layoutChanged = (mLayout.getWidth() != want) ||
6934 (hintWidth != hintWant) ||
6935 (mLayout.getEllipsizedWidth() !=
6936 width - getCompoundPaddingLeft() - getCompoundPaddingRight());
6938 final boolean widthChanged = (mHint == null) &&
6939 (mEllipsize == null) &&
6940 (want > mLayout.getWidth()) &&
6941 (mLayout instanceof BoringLayout || (fromexisting && des >= 0 && des <= want));
6943 final boolean maximumChanged = (mMaxMode != mOldMaxMode) || (mMaximum != mOldMaximum);
6945 if (layoutChanged || maximumChanged) {
6946 if (!maximumChanged && widthChanged) {
6947 mLayout.increaseWidthTo(want);
6949 makeNewLayout(want, hintWant, boring, hintBoring,
6950 width - getCompoundPaddingLeft() - getCompoundPaddingRight(), false);
6953 // Nothing has changed
6957 if (heightMode == MeasureSpec.EXACTLY) {
6958 // Parent has told us how big to be. So be it.
6959 height = heightSize;
6960 mDesiredHeightAtMeasure = -1;
6962 int desired = getDesiredHeight();
6965 mDesiredHeightAtMeasure = desired;
6967 if (heightMode == MeasureSpec.AT_MOST) {
6968 height = Math.min(desired, heightSize);
6972 int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom();
6973 if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) {
6974 unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum));
6978 * We didn't let makeNewLayout() register to bring the cursor into view,
6979 * so do it here if there is any possibility that it is needed.
6981 if (mMovement != null ||
6982 mLayout.getWidth() > unpaddedWidth ||
6983 mLayout.getHeight() > unpaddedHeight) {
6984 registerForPreDraw();
6989 setMeasuredDimension(width, height);
6992 private int getDesiredHeight() {
6994 getDesiredHeight(mLayout, true),
6995 getDesiredHeight(mHintLayout, mEllipsize != null));
6998 private int getDesiredHeight(Layout layout, boolean cap) {
6999 if (layout == null) {
7003 int linecount = layout.getLineCount();
7004 int pad = getCompoundPaddingTop() + getCompoundPaddingBottom();
7005 int desired = layout.getLineTop(linecount);
7007 final Drawables dr = mDrawables;
7009 desired = Math.max(desired, dr.mDrawableHeightLeft);
7010 desired = Math.max(desired, dr.mDrawableHeightRight);
7015 if (mMaxMode == LINES) {
7017 * Don't cap the hint to a certain number of lines.
7018 * (Do cap it, though, if we have a maximum pixel height.)
7021 if (linecount > mMaximum) {
7022 desired = layout.getLineTop(mMaximum);
7025 desired = Math.max(desired, dr.mDrawableHeightLeft);
7026 desired = Math.max(desired, dr.mDrawableHeightRight);
7030 linecount = mMaximum;
7034 desired = Math.min(desired, mMaximum);
7037 if (mMinMode == LINES) {
7038 if (linecount < mMinimum) {
7039 desired += getLineHeight() * (mMinimum - linecount);
7042 desired = Math.max(desired, mMinimum);
7045 // Check against our minimum height
7046 desired = Math.max(desired, getSuggestedMinimumHeight());
7052 * Check whether a change to the existing text layout requires a
7055 private void checkForResize() {
7056 boolean sizeChanged = false;
7058 if (mLayout != null) {
7059 // Check if our width changed
7060 if (mLayoutParams.width == LayoutParams.WRAP_CONTENT) {
7065 // Check if our height changed
7066 if (mLayoutParams.height == LayoutParams.WRAP_CONTENT) {
7067 int desiredHeight = getDesiredHeight();
7069 if (desiredHeight != this.getHeight()) {
7072 } else if (mLayoutParams.height == LayoutParams.MATCH_PARENT) {
7073 if (mDesiredHeightAtMeasure >= 0) {
7074 int desiredHeight = getDesiredHeight();
7076 if (desiredHeight != mDesiredHeightAtMeasure) {
7085 // caller will have already invalidated
7090 * Check whether entirely new text requires a new view layout
7091 * or merely a new text layout.
7093 private void checkForRelayout() {
7094 // If we have a fixed width, we can just swap in a new text layout
7095 // if the text height stays the same or if the view height is fixed.
7097 if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||
7098 (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&
7099 (mHint == null || mHintLayout != null) &&
7100 (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {
7101 // Static width, so try making a new text layout.
7103 int oldht = mLayout.getHeight();
7104 int want = mLayout.getWidth();
7105 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();
7108 * No need to bring the text into view, since the size is not
7109 * changing (unless we do the requestLayout(), in which case it
7110 * will happen at measure).
7112 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
7113 mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),
7116 if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {
7117 // In a fixed-height view, so use our new text layout.
7118 if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&
7119 mLayoutParams.height != LayoutParams.MATCH_PARENT) {
7124 // Dynamic height, but height has stayed the same,
7125 // so use our new text layout.
7126 if (mLayout.getHeight() == oldht &&
7127 (mHintLayout == null || mHintLayout.getHeight() == oldht)) {
7133 // We lose: the height has changed and we have a dynamic height.
7134 // Request a new view layout using our new text layout.
7138 // Dynamic width, so we have no choice but to request a new
7139 // view layout with a new text layout.
7147 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
7148 super.onLayout(changed, left, top, right, bottom);
7149 if (mDeferScroll >= 0) {
7150 int curs = mDeferScroll;
7152 bringPointIntoView(Math.min(curs, mText.length()));
7156 private boolean isShowingHint() {
7157 return TextUtils.isEmpty(mText) && !TextUtils.isEmpty(mHint);
7161 * Returns true if anything changed.
7163 private boolean bringTextIntoView() {
7164 Layout layout = isShowingHint() ? mHintLayout : mLayout;
7166 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
7167 line = layout.getLineCount() - 1;
7170 Layout.Alignment a = layout.getParagraphAlignment(line);
7171 int dir = layout.getParagraphDirection(line);
7172 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
7173 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
7174 int ht = layout.getHeight();
7176 int scrollx, scrolly;
7178 // Convert to left, center, or right alignment.
7179 if (a == Layout.Alignment.ALIGN_NORMAL) {
7180 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_LEFT :
7181 Layout.Alignment.ALIGN_RIGHT;
7182 } else if (a == Layout.Alignment.ALIGN_OPPOSITE){
7183 a = dir == Layout.DIR_LEFT_TO_RIGHT ? Layout.Alignment.ALIGN_RIGHT :
7184 Layout.Alignment.ALIGN_LEFT;
7187 if (a == Layout.Alignment.ALIGN_CENTER) {
7189 * Keep centered if possible, or, if it is too wide to fit,
7190 * keep leading edge in view.
7193 int left = (int) Math.floor(layout.getLineLeft(line));
7194 int right = (int) Math.ceil(layout.getLineRight(line));
7196 if (right - left < hspace) {
7197 scrollx = (right + left) / 2 - hspace / 2;
7200 scrollx = right - hspace;
7205 } else if (a == Layout.Alignment.ALIGN_RIGHT) {
7206 int right = (int) Math.ceil(layout.getLineRight(line));
7207 scrollx = right - hspace;
7208 } else { // a == Layout.Alignment.ALIGN_LEFT (will also be the default)
7209 scrollx = (int) Math.floor(layout.getLineLeft(line));
7215 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
7216 scrolly = ht - vspace;
7222 if (scrollx != mScrollX || scrolly != mScrollY) {
7223 scrollTo(scrollx, scrolly);
7231 * Move the point, specified by the offset, into the view if it is needed.
7232 * This has to be called after layout. Returns true if anything changed.
7234 public boolean bringPointIntoView(int offset) {
7235 if (isLayoutRequested()) {
7236 mDeferScroll = offset;
7239 boolean changed = false;
7241 Layout layout = isShowingHint() ? mHintLayout: mLayout;
7243 if (layout == null) return changed;
7245 int line = layout.getLineForOffset(offset);
7249 switch (layout.getParagraphAlignment(line)) {
7257 grav = layout.getParagraphDirection(line);
7259 case ALIGN_OPPOSITE:
7260 grav = -layout.getParagraphDirection(line);
7268 // We only want to clamp the cursor to fit within the layout width
7269 // in left-to-right modes, because in a right to left alignment,
7270 // we want to scroll to keep the line-right on the screen, as other
7271 // lines are likely to have text flush with the right margin, which
7272 // we want to keep visible.
7273 // A better long-term solution would probably be to measure both
7274 // the full line and a blank-trimmed version, and, for example, use
7275 // the latter measurement for centering and right alignment, but for
7276 // the time being we only implement the cursor clamping in left to
7277 // right where it is most likely to be annoying.
7278 final boolean clamped = grav > 0;
7279 // FIXME: Is it okay to truncate this, or should we round?
7280 final int x = (int)layout.getPrimaryHorizontal(offset, clamped);
7281 final int top = layout.getLineTop(line);
7282 final int bottom = layout.getLineTop(line + 1);
7284 int left = (int) Math.floor(layout.getLineLeft(line));
7285 int right = (int) Math.ceil(layout.getLineRight(line));
7286 int ht = layout.getHeight();
7288 int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
7289 int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
7290 if (!mHorizontallyScrolling && right - left > hspace && right > x) {
7291 // If cursor has been clamped, make sure we don't scroll.
7292 right = Math.max(x, left + hspace);
7295 int hslack = (bottom - top) / 2;
7296 int vslack = hslack;
7298 if (vslack > vspace / 4)
7299 vslack = vspace / 4;
7300 if (hslack > hspace / 4)
7301 hslack = hspace / 4;
7306 if (top - vs < vslack)
7308 if (bottom - vs > vspace - vslack)
7309 vs = bottom - (vspace - vslack);
7310 if (ht - vs < vspace)
7316 if (x - hs < hslack) {
7319 if (x - hs > hspace - hslack) {
7320 hs = x - (hspace - hslack);
7327 if (right - hs < hspace)
7328 hs = right - hspace;
7329 } else if (grav > 0) {
7330 if (right - hs < hspace)
7331 hs = right - hspace;
7334 } else /* grav == 0 */ {
7335 if (right - left <= hspace) {
7337 * If the entire text fits, center it exactly.
7339 hs = left - (hspace - (right - left)) / 2;
7340 } else if (x > right - hslack) {
7342 * If we are near the right edge, keep the right edge
7343 * at the edge of the view.
7345 hs = right - hspace;
7346 } else if (x < left + hslack) {
7348 * If we are near the left edge, keep the left edge
7349 * at the edge of the view.
7352 } else if (left > hs) {
7354 * Is there whitespace visible at the left? Fix it if so.
7357 } else if (right < hs + hspace) {
7359 * Is there whitespace visible at the right? Fix it if so.
7361 hs = right - hspace;
7364 * Otherwise, float as needed.
7366 if (x - hs < hslack) {
7369 if (x - hs > hspace - hslack) {
7370 hs = x - (hspace - hslack);
7375 if (hs != mScrollX || vs != mScrollY) {
7376 if (mScroller == null) {
7379 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
7380 int dx = hs - mScrollX;
7381 int dy = vs - mScrollY;
7383 if (duration > ANIMATED_SCROLL_GAP) {
7384 mScroller.startScroll(mScrollX, mScrollY, dx, dy);
7385 awakenScrollBars(mScroller.getDuration());
7388 if (!mScroller.isFinished()) {
7389 mScroller.abortAnimation();
7395 mLastScroll = AnimationUtils.currentAnimationTimeMillis();
7402 // This offsets because getInterestingRect() is in terms of viewport coordinates, but
7403 // requestRectangleOnScreen() is in terms of content coordinates.
7405 // The offsets here are to ensure the rectangle we are using is
7406 // within our view bounds, in case the cursor is on the far left
7407 // or right. If it isn't withing the bounds, then this request
7409 if (mTempRect == null) mTempRect = new Rect();
7410 mTempRect.set(x - 2, top, x + 2, bottom);
7411 getInterestingRect(mTempRect, line);
7412 mTempRect.offset(mScrollX, mScrollY);
7414 if (requestRectangleOnScreen(mTempRect)) {
7423 * Move the cursor, if needed, so that it is at an offset that is visible
7424 * to the user. This will not move the cursor if it represents more than
7425 * one character (a selection range). This will only work if the
7426 * TextView contains spannable text; otherwise it will do nothing.
7428 * @return True if the cursor was actually moved, false otherwise.
7430 public boolean moveCursorToVisibleOffset() {
7431 if (!(mText instanceof Spannable)) {
7434 int start = getSelectionStart();
7435 int end = getSelectionEnd();
7440 // First: make sure the line is visible on screen:
7442 int line = mLayout.getLineForOffset(start);
7444 final int top = mLayout.getLineTop(line);
7445 final int bottom = mLayout.getLineTop(line + 1);
7446 final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
7447 int vslack = (bottom - top) / 2;
7448 if (vslack > vspace / 4)
7449 vslack = vspace / 4;
7450 final int vs = mScrollY;
7452 if (top < (vs+vslack)) {
7453 line = mLayout.getLineForVertical(vs+vslack+(bottom-top));
7454 } else if (bottom > (vspace+vs-vslack)) {
7455 line = mLayout.getLineForVertical(vspace+vs-vslack-(bottom-top));
7458 // Next: make sure the character is visible on screen:
7460 final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
7461 final int hs = mScrollX;
7462 final int leftChar = mLayout.getOffsetForHorizontal(line, hs);
7463 final int rightChar = mLayout.getOffsetForHorizontal(line, hspace+hs);
7465 // line might contain bidirectional text
7466 final int lowChar = leftChar < rightChar ? leftChar : rightChar;
7467 final int highChar = leftChar > rightChar ? leftChar : rightChar;
7469 int newStart = start;
7470 if (newStart < lowChar) {
7472 } else if (newStart > highChar) {
7473 newStart = highChar;
7476 if (newStart != start) {
7477 Selection.setSelection((Spannable)mText, newStart);
7485 public void computeScroll() {
7486 if (mScroller != null) {
7487 if (mScroller.computeScrollOffset()) {
7488 mScrollX = mScroller.getCurrX();
7489 mScrollY = mScroller.getCurrY();
7490 invalidateParentCaches();
7491 postInvalidate(); // So we draw again
7496 private void getInterestingRect(Rect r, int line) {
7497 convertFromViewportToContentCoordinates(r);
7499 // Rectangle can can be expanded on first and last line to take
7500 // padding into account.
7501 // TODO Take left/right padding into account too?
7502 if (line == 0) r.top -= getExtendedPaddingTop();
7503 if (line == mLayout.getLineCount() - 1) r.bottom += getExtendedPaddingBottom();
7506 private void convertFromViewportToContentCoordinates(Rect r) {
7507 final int horizontalOffset = viewportToContentHorizontalOffset();
7508 r.left += horizontalOffset;
7509 r.right += horizontalOffset;
7511 final int verticalOffset = viewportToContentVerticalOffset();
7512 r.top += verticalOffset;
7513 r.bottom += verticalOffset;
7516 int viewportToContentHorizontalOffset() {
7517 return getCompoundPaddingLeft() - mScrollX;
7520 int viewportToContentVerticalOffset() {
7521 int offset = getExtendedPaddingTop() - mScrollY;
7522 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.TOP) {
7523 offset += getVerticalOffset(false);
7529 public void debug(int depth) {
7532 String output = debugIndent(depth);
7533 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
7534 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
7537 if (mText != null) {
7539 output += "mText=\"" + mText + "\" ";
7540 if (mLayout != null) {
7541 output += "mLayout width=" + mLayout.getWidth()
7542 + " height=" + mLayout.getHeight();
7545 output += "mText=NULL";
7547 Log.d(VIEW_LOG_TAG, output);
7551 * Convenience for {@link Selection#getSelectionStart}.
7553 @ViewDebug.ExportedProperty(category = "text")
7554 public int getSelectionStart() {
7555 return Selection.getSelectionStart(getText());
7559 * Convenience for {@link Selection#getSelectionEnd}.
7561 @ViewDebug.ExportedProperty(category = "text")
7562 public int getSelectionEnd() {
7563 return Selection.getSelectionEnd(getText());
7567 * Return true iff there is a selection inside this text view.
7569 public boolean hasSelection() {
7570 final int selectionStart = getSelectionStart();
7571 final int selectionEnd = getSelectionEnd();
7573 return selectionStart >= 0 && selectionStart != selectionEnd;
7576 String getSelectedText() {
7577 if (hasSelection()) {
7578 return String.valueOf(mText.subSequence(getSelectionStart(), getSelectionEnd()));
7584 * Sets the properties of this field (lines, horizontally scrolling,
7585 * transformation method) to be for a single-line input.
7587 * @attr ref android.R.styleable#TextView_singleLine
7589 public void setSingleLine() {
7590 setSingleLine(true);
7594 * Sets the properties of this field to transform input to ALL CAPS
7595 * display. This may use a "small caps" formatting if available.
7596 * This setting will be ignored if this field is editable or selectable.
7598 * This call replaces the current transformation method. Disabling this
7599 * will not necessarily restore the previous behavior from before this
7602 * @see #setTransformationMethod(TransformationMethod)
7603 * @attr ref android.R.styleable#TextView_textAllCaps
7605 public void setAllCaps(boolean allCaps) {
7607 setTransformationMethod(new AllCapsTransformationMethod(getContext()));
7609 setTransformationMethod(null);
7614 * If true, sets the properties of this field (number of lines, horizontally scrolling,
7615 * transformation method) to be for a single-line input; if false, restores these to the default
7618 * Note that the default conditions are not necessarily those that were in effect prior this
7619 * method, and you may want to reset these properties to your custom values.
7621 * @attr ref android.R.styleable#TextView_singleLine
7623 @android.view.RemotableViewMethod
7624 public void setSingleLine(boolean singleLine) {
7625 // Could be used, but may break backward compatibility.
7626 // if (mSingleLine == singleLine) return;
7627 setInputTypeSingleLine(singleLine);
7628 applySingleLine(singleLine, true, true);
7632 * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType.
7635 private void setInputTypeSingleLine(boolean singleLine) {
7636 if (mEditor != null &&
7637 (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
7639 mEditor.mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
7641 mEditor.mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
7646 private void applySingleLine(boolean singleLine, boolean applyTransformation,
7647 boolean changeMaxLines) {
7648 mSingleLine = singleLine;
7651 setHorizontallyScrolling(true);
7652 if (applyTransformation) {
7653 setTransformationMethod(SingleLineTransformationMethod.getInstance());
7656 if (changeMaxLines) {
7657 setMaxLines(Integer.MAX_VALUE);
7659 setHorizontallyScrolling(false);
7660 if (applyTransformation) {
7661 setTransformationMethod(null);
7667 * Causes words in the text that are longer than the view is wide
7668 * to be ellipsized instead of broken in the middle. You may also
7669 * want to {@link #setSingleLine} or {@link #setHorizontallyScrolling}
7670 * to constrain the text to a single line. Use <code>null</code>
7671 * to turn off ellipsizing.
7673 * If {@link #setMaxLines} has been used to set two or more lines,
7674 * only {@link android.text.TextUtils.TruncateAt#END} and
7675 * {@link android.text.TextUtils.TruncateAt#MARQUEE} are supported
7676 * (other ellipsizing types will not do anything).
7678 * @attr ref android.R.styleable#TextView_ellipsize
7680 public void setEllipsize(TextUtils.TruncateAt where) {
7681 // TruncateAt is an enum. != comparison is ok between these singleton objects.
7682 if (mEllipsize != where) {
7685 if (mLayout != null) {
7694 * Sets how many times to repeat the marquee animation. Only applied if the
7695 * TextView has marquee enabled. Set to -1 to repeat indefinitely.
7697 * @see #getMarqueeRepeatLimit()
7699 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
7701 public void setMarqueeRepeatLimit(int marqueeLimit) {
7702 mMarqueeRepeatLimit = marqueeLimit;
7706 * Gets the number of times the marquee animation is repeated. Only meaningful if the
7707 * TextView has marquee enabled.
7709 * @return the number of times the marquee animation is repeated. -1 if the animation
7710 * repeats indefinitely
7712 * @see #setMarqueeRepeatLimit(int)
7714 * @attr ref android.R.styleable#TextView_marqueeRepeatLimit
7716 public int getMarqueeRepeatLimit() {
7717 return mMarqueeRepeatLimit;
7721 * Returns where, if anywhere, words that are longer than the view
7722 * is wide should be ellipsized.
7724 @ViewDebug.ExportedProperty
7725 public TextUtils.TruncateAt getEllipsize() {
7730 * Set the TextView so that when it takes focus, all the text is
7733 * @attr ref android.R.styleable#TextView_selectAllOnFocus
7735 @android.view.RemotableViewMethod
7736 public void setSelectAllOnFocus(boolean selectAllOnFocus) {
7737 createEditorIfNeeded();
7738 mEditor.mSelectAllOnFocus = selectAllOnFocus;
7740 if (selectAllOnFocus && !(mText instanceof Spannable)) {
7741 setText(mText, BufferType.SPANNABLE);
7746 * Set whether the cursor is visible. The default is true. Note that this property only
7747 * makes sense for editable TextView.
7749 * @see #isCursorVisible()
7751 * @attr ref android.R.styleable#TextView_cursorVisible
7753 @android.view.RemotableViewMethod
7754 public void setCursorVisible(boolean visible) {
7755 if (visible && mEditor == null) return; // visible is the default value with no edit data
7756 createEditorIfNeeded();
7757 if (mEditor.mCursorVisible != visible) {
7758 mEditor.mCursorVisible = visible;
7761 mEditor.makeBlink();
7763 // InsertionPointCursorController depends on mCursorVisible
7764 mEditor.prepareCursorControllers();
7769 * @return whether or not the cursor is visible (assuming this TextView is editable)
7771 * @see #setCursorVisible(boolean)
7773 * @attr ref android.R.styleable#TextView_cursorVisible
7775 public boolean isCursorVisible() {
7776 // true is the default value
7777 return mEditor == null ? true : mEditor.mCursorVisible;
7780 private boolean canMarquee() {
7781 int width = (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight());
7782 return width > 0 && (mLayout.getLineWidth(0) > width ||
7783 (mMarqueeFadeMode != MARQUEE_FADE_NORMAL && mSavedMarqueeModeLayout != null &&
7784 mSavedMarqueeModeLayout.getLineWidth(0) > width));
7787 private void startMarquee() {
7788 // Do not ellipsize EditText
7789 if (getKeyListener() != null) return;
7791 if (compressText(getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight())) {
7795 if ((mMarquee == null || mMarquee.isStopped()) && (isFocused() || isSelected()) &&
7796 getLineCount() == 1 && canMarquee()) {
7798 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
7799 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_FADE;
7800 final Layout tmp = mLayout;
7801 mLayout = mSavedMarqueeModeLayout;
7802 mSavedMarqueeModeLayout = tmp;
7803 setHorizontalFadingEdgeEnabled(true);
7808 if (mMarquee == null) mMarquee = new Marquee(this);
7809 mMarquee.start(mMarqueeRepeatLimit);
7813 private void stopMarquee() {
7814 if (mMarquee != null && !mMarquee.isStopped()) {
7818 if (mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_FADE) {
7819 mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
7820 final Layout tmp = mSavedMarqueeModeLayout;
7821 mSavedMarqueeModeLayout = mLayout;
7823 setHorizontalFadingEdgeEnabled(false);
7829 private void startStopMarquee(boolean start) {
7830 if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
7840 * This method is called when the text is changed, in case any subclasses
7841 * would like to know.
7843 * Within <code>text</code>, the <code>lengthAfter</code> characters
7844 * beginning at <code>start</code> have just replaced old text that had
7845 * length <code>lengthBefore</code>. It is an error to attempt to make
7846 * changes to <code>text</code> from this callback.
7848 * @param text The text the TextView is displaying
7849 * @param start The offset of the start of the range of the text that was
7851 * @param lengthBefore The length of the former text that has been replaced
7852 * @param lengthAfter The length of the replacement modified text
7854 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
7855 // intentionally empty, template pattern method can be overridden by subclasses
7859 * This method is called when the selection has changed, in case any
7860 * subclasses would like to know.
7862 * @param selStart The new selection start location.
7863 * @param selEnd The new selection end location.
7865 protected void onSelectionChanged(int selStart, int selEnd) {
7866 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED);
7870 * Adds a TextWatcher to the list of those whose methods are called
7871 * whenever this TextView's text changes.
7873 * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously
7874 * not called after {@link #setText} calls. Now, doing {@link #setText}
7875 * if there are any text changed listeners forces the buffer type to
7876 * Editable if it would not otherwise be and does call this method.
7878 public void addTextChangedListener(TextWatcher watcher) {
7879 if (mListeners == null) {
7880 mListeners = new ArrayList<TextWatcher>();
7883 mListeners.add(watcher);
7887 * Removes the specified TextWatcher from the list of those whose
7888 * methods are called
7889 * whenever this TextView's text changes.
7891 public void removeTextChangedListener(TextWatcher watcher) {
7892 if (mListeners != null) {
7893 int i = mListeners.indexOf(watcher);
7896 mListeners.remove(i);
7901 private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) {
7902 if (mListeners != null) {
7903 final ArrayList<TextWatcher> list = mListeners;
7904 final int count = list.size();
7905 for (int i = 0; i < count; i++) {
7906 list.get(i).beforeTextChanged(text, start, before, after);
7910 // The spans that are inside or intersect the modified region no longer make sense
7911 removeIntersectingNonAdjacentSpans(start, start + before, SpellCheckSpan.class);
7912 removeIntersectingNonAdjacentSpans(start, start + before, SuggestionSpan.class);
7915 // Removes all spans that are inside or actually overlap the start..end range
7916 private <T> void removeIntersectingNonAdjacentSpans(int start, int end, Class<T> type) {
7917 if (!(mText instanceof Editable)) return;
7918 Editable text = (Editable) mText;
7920 T[] spans = text.getSpans(start, end, type);
7921 final int length = spans.length;
7922 for (int i = 0; i < length; i++) {
7923 final int spanStart = text.getSpanStart(spans[i]);
7924 final int spanEnd = text.getSpanEnd(spans[i]);
7925 if (spanEnd == start || spanStart == end) break;
7926 text.removeSpan(spans[i]);
7930 void removeAdjacentSuggestionSpans(final int pos) {
7931 if (!(mText instanceof Editable)) return;
7932 final Editable text = (Editable) mText;
7934 final SuggestionSpan[] spans = text.getSpans(pos, pos, SuggestionSpan.class);
7935 final int length = spans.length;
7936 for (int i = 0; i < length; i++) {
7937 final int spanStart = text.getSpanStart(spans[i]);
7938 final int spanEnd = text.getSpanEnd(spans[i]);
7939 if (spanEnd == pos || spanStart == pos) {
7940 if (SpellChecker.haveWordBoundariesChanged(text, pos, pos, spanStart, spanEnd)) {
7941 text.removeSpan(spans[i]);
7948 * Not private so it can be called from an inner class without going
7951 void sendOnTextChanged(CharSequence text, int start, int before, int after) {
7952 if (mListeners != null) {
7953 final ArrayList<TextWatcher> list = mListeners;
7954 final int count = list.size();
7955 for (int i = 0; i < count; i++) {
7956 list.get(i).onTextChanged(text, start, before, after);
7960 if (mEditor != null) mEditor.sendOnTextChanged(start, after);
7964 * Not private so it can be called from an inner class without going
7967 void sendAfterTextChanged(Editable text) {
7968 if (mListeners != null) {
7969 final ArrayList<TextWatcher> list = mListeners;
7970 final int count = list.size();
7971 for (int i = 0; i < count; i++) {
7972 list.get(i).afterTextChanged(text);
7975 hideErrorIfUnchanged();
7978 void updateAfterEdit() {
7980 int curs = getSelectionStart();
7982 if (curs >= 0 || (mGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
7983 registerForPreDraw();
7989 mHighlightPathBogus = true;
7990 if (mEditor != null) mEditor.makeBlink();
7991 bringPointIntoView(curs);
7996 * Not private so it can be called from an inner class without going
7999 void handleTextChanged(CharSequence buffer, int start, int before, int after) {
8000 sLastCutCopyOrTextChangedTime = 0;
8002 final Editor.InputMethodState ims = mEditor == null ? null : mEditor.mInputMethodState;
8003 if (ims == null || ims.mBatchEditNesting == 0) {
8007 ims.mContentChanged = true;
8008 if (ims.mChangedStart < 0) {
8009 ims.mChangedStart = start;
8010 ims.mChangedEnd = start+before;
8012 ims.mChangedStart = Math.min(ims.mChangedStart, start);
8013 ims.mChangedEnd = Math.max(ims.mChangedEnd, start + before - ims.mChangedDelta);
8015 ims.mChangedDelta += after-before;
8017 resetErrorChangedFlag();
8018 sendOnTextChanged(buffer, start, before, after);
8019 onTextChanged(buffer, start, before, after);
8023 * Not private so it can be called from an inner class without going
8026 void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) {
8027 // XXX Make the start and end move together if this ends up
8028 // spending too much time invalidating.
8030 boolean selChanged = false;
8031 int newSelStart=-1, newSelEnd=-1;
8033 final Editor.InputMethodState ims = mEditor == null ? null : mEditor.mInputMethodState;
8035 if (what == Selection.SELECTION_END) {
8037 newSelEnd = newStart;
8039 if (oldStart >= 0 || newStart >= 0) {
8040 invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart);
8042 registerForPreDraw();
8043 if (mEditor != null) mEditor.makeBlink();
8047 if (what == Selection.SELECTION_START) {
8049 newSelStart = newStart;
8051 if (oldStart >= 0 || newStart >= 0) {
8052 int end = Selection.getSelectionEnd(buf);
8053 invalidateCursor(end, oldStart, newStart);
8058 mHighlightPathBogus = true;
8059 if (mEditor != null && !isFocused()) mEditor.mSelectionMoved = true;
8061 if ((buf.getSpanFlags(what)&Spanned.SPAN_INTERMEDIATE) == 0) {
8062 if (newSelStart < 0) {
8063 newSelStart = Selection.getSelectionStart(buf);
8065 if (newSelEnd < 0) {
8066 newSelEnd = Selection.getSelectionEnd(buf);
8068 onSelectionChanged(newSelStart, newSelEnd);
8072 if (what instanceof UpdateAppearance || what instanceof ParagraphStyle ||
8073 what instanceof CharacterStyle) {
8074 if (ims == null || ims.mBatchEditNesting == 0) {
8076 mHighlightPathBogus = true;
8079 ims.mContentChanged = true;
8081 if (mEditor != null) {
8082 if (oldStart >= 0) mEditor.invalidateTextDisplayList(mLayout, oldStart, oldEnd);
8083 if (newStart >= 0) mEditor.invalidateTextDisplayList(mLayout, newStart, newEnd);
8087 if (MetaKeyKeyListener.isMetaTracker(buf, what)) {
8088 mHighlightPathBogus = true;
8089 if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) {
8090 ims.mSelectionModeChanged = true;
8093 if (Selection.getSelectionStart(buf) >= 0) {
8094 if (ims == null || ims.mBatchEditNesting == 0) {
8097 ims.mCursorChanged = true;
8102 if (what instanceof ParcelableSpan) {
8103 // If this is a span that can be sent to a remote process,
8104 // the current extract editor would be interested in it.
8105 if (ims != null && ims.mExtractedTextRequest != null) {
8106 if (ims.mBatchEditNesting != 0) {
8107 if (oldStart >= 0) {
8108 if (ims.mChangedStart > oldStart) {
8109 ims.mChangedStart = oldStart;
8111 if (ims.mChangedStart > oldEnd) {
8112 ims.mChangedStart = oldEnd;
8115 if (newStart >= 0) {
8116 if (ims.mChangedStart > newStart) {
8117 ims.mChangedStart = newStart;
8119 if (ims.mChangedStart > newEnd) {
8120 ims.mChangedStart = newEnd;
8124 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "Span change outside of batch: "
8125 + oldStart + "-" + oldEnd + ","
8126 + newStart + "-" + newEnd + " " + what);
8127 ims.mContentChanged = true;
8132 if (mEditor != null && mEditor.mSpellChecker != null && newStart < 0 &&
8133 what instanceof SpellCheckSpan) {
8134 mEditor.mSpellChecker.onSpellCheckSpanRemoved((SpellCheckSpan) what);
8142 public void dispatchFinishTemporaryDetach() {
8143 mDispatchTemporaryDetach = true;
8144 super.dispatchFinishTemporaryDetach();
8145 mDispatchTemporaryDetach = false;
8149 public void onStartTemporaryDetach() {
8150 super.onStartTemporaryDetach();
8151 // Only track when onStartTemporaryDetach() is called directly,
8152 // usually because this instance is an editable field in a list
8153 if (!mDispatchTemporaryDetach) mTemporaryDetach = true;
8155 // Tell the editor that we are temporarily detached. It can use this to preserve
8156 // selection state as needed.
8157 if (mEditor != null) mEditor.mTemporaryDetach = true;
8161 public void onFinishTemporaryDetach() {
8162 super.onFinishTemporaryDetach();
8163 // Only track when onStartTemporaryDetach() is called directly,
8164 // usually because this instance is an editable field in a list
8165 if (!mDispatchTemporaryDetach) mTemporaryDetach = false;
8166 if (mEditor != null) mEditor.mTemporaryDetach = false;
8170 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
8171 if (mTemporaryDetach) {
8172 // If we are temporarily in the detach state, then do nothing.
8173 super.onFocusChanged(focused, direction, previouslyFocusedRect);
8177 if (mEditor != null) mEditor.onFocusChanged(focused, direction);
8180 if (mText instanceof Spannable) {
8181 Spannable sp = (Spannable) mText;
8182 MetaKeyKeyListener.resetMetaState(sp);
8186 startStopMarquee(focused);
8188 if (mTransformation != null) {
8189 mTransformation.onFocusChanged(this, mText, focused, direction, previouslyFocusedRect);
8192 super.onFocusChanged(focused, direction, previouslyFocusedRect);
8196 public void onWindowFocusChanged(boolean hasWindowFocus) {
8197 super.onWindowFocusChanged(hasWindowFocus);
8199 if (mEditor != null) mEditor.onWindowFocusChanged(hasWindowFocus);
8201 startStopMarquee(hasWindowFocus);
8205 protected void onVisibilityChanged(View changedView, int visibility) {
8206 super.onVisibilityChanged(changedView, visibility);
8207 if (mEditor != null && visibility != VISIBLE) {
8208 mEditor.hideCursorAndSpanControllers();
8209 stopTextActionMode();
8214 * Use {@link BaseInputConnection#removeComposingSpans
8215 * BaseInputConnection.removeComposingSpans()} to remove any IME composing
8216 * state from this text view.
8218 public void clearComposingText() {
8219 if (mText instanceof Spannable) {
8220 BaseInputConnection.removeComposingSpans((Spannable)mText);
8225 public void setSelected(boolean selected) {
8226 boolean wasSelected = isSelected();
8228 super.setSelected(selected);
8230 if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
8240 public boolean onTouchEvent(MotionEvent event) {
8241 final int action = event.getActionMasked();
8243 if (mEditor != null && action == MotionEvent.ACTION_DOWN) {
8244 // Detect double tap and inform the Editor.
8245 if (mFirstTouch && (SystemClock.uptimeMillis() - mLastTouchUpTime) <=
8246 ViewConfiguration.getDoubleTapTimeout()) {
8247 mEditor.mDoubleTap = true;
8248 mFirstTouch = false;
8250 mEditor.mDoubleTap = false;
8255 if (action == MotionEvent.ACTION_UP) {
8256 mLastTouchUpTime = SystemClock.uptimeMillis();
8259 if (mEditor != null) {
8260 mEditor.onTouchEvent(event);
8262 if (mEditor.mSelectionModifierCursorController != null &&
8263 mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
8268 final boolean superResult = super.onTouchEvent(event);
8271 * Don't handle the release after a long press, because it will move the selection away from
8272 * whatever the menu action was trying to affect. If the long press should have triggered an
8273 * insertion action mode, we can now actually show it.
8275 if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
8276 mEditor.mDiscardNextActionUp = false;
8278 if (mEditor.mIsInsertionActionModeStartPending) {
8279 mEditor.startInsertionActionMode();
8280 mEditor.mIsInsertionActionModeStartPending = false;
8285 final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) &&
8286 (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
8288 if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
8289 && mText instanceof Spannable && mLayout != null) {
8290 boolean handled = false;
8292 if (mMovement != null) {
8293 handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
8296 final boolean textIsSelectable = isTextSelectable();
8297 if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
8298 // The LinkMovementMethod which should handle taps on links has not been installed
8299 // on non editable text that support text selection.
8300 // We reproduce its behavior here to open links for these.
8301 ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
8302 getSelectionEnd(), ClickableSpan.class);
8304 if (links.length > 0) {
8305 links[0].onClick(this);
8310 if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
8311 // Show the IME, except when selecting in read-only text.
8312 final InputMethodManager imm = InputMethodManager.peekInstance();
8314 if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
8315 handled |= imm != null && imm.showSoftInput(this, 0);
8318 // The above condition ensures that the mEditor is not null
8319 mEditor.onTouchUpEvent(event);
8333 public boolean onGenericMotionEvent(MotionEvent event) {
8334 if (mMovement != null && mText instanceof Spannable && mLayout != null) {
8336 if (mMovement.onGenericMotionEvent(this, (Spannable) mText, event)) {
8339 } catch (AbstractMethodError ex) {
8340 // onGenericMotionEvent was added to the MovementMethod interface in API 12.
8341 // Ignore its absence in case third party applications implemented the
8342 // interface directly.
8345 return super.onGenericMotionEvent(event);
8349 * @return True iff this TextView contains a text that can be edited, or if this is
8350 * a selectable TextView.
8352 boolean isTextEditable() {
8353 return mText instanceof Editable && onCheckIsTextEditor() && isEnabled();
8357 * Returns true, only while processing a touch gesture, if the initial
8358 * touch down event caused focus to move to the text view and as a result
8359 * its selection changed. Only valid while processing the touch gesture
8360 * of interest, in an editable text view.
8362 public boolean didTouchFocusSelect() {
8363 return mEditor != null && mEditor.mTouchFocusSelected;
8367 public void cancelLongPress() {
8368 super.cancelLongPress();
8369 if (mEditor != null) mEditor.mIgnoreActionUpEvent = true;
8373 public boolean onTrackballEvent(MotionEvent event) {
8374 if (mMovement != null && mText instanceof Spannable && mLayout != null) {
8375 if (mMovement.onTrackballEvent(this, (Spannable) mText, event)) {
8380 return super.onTrackballEvent(event);
8383 public void setScroller(Scroller s) {
8388 protected float getLeftFadingEdgeStrength() {
8389 if (mEllipsize == TextUtils.TruncateAt.MARQUEE &&
8390 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
8391 if (mMarquee != null && !mMarquee.isStopped()) {
8392 final Marquee marquee = mMarquee;
8393 if (marquee.shouldDrawLeftFade()) {
8394 final float scroll = marquee.getScroll();
8395 return scroll / getHorizontalFadingEdgeLength();
8399 } else if (getLineCount() == 1) {
8400 final int layoutDirection = getLayoutDirection();
8401 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
8402 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
8406 return (mLayout.getLineRight(0) - (mRight - mLeft) -
8407 getCompoundPaddingLeft() - getCompoundPaddingRight() -
8408 mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength();
8409 case Gravity.CENTER_HORIZONTAL:
8410 case Gravity.FILL_HORIZONTAL:
8411 final int textDirection = mLayout.getParagraphDirection(0);
8412 if (textDirection == Layout.DIR_LEFT_TO_RIGHT) {
8415 return (mLayout.getLineRight(0) - (mRight - mLeft) -
8416 getCompoundPaddingLeft() - getCompoundPaddingRight() -
8417 mLayout.getLineLeft(0)) / getHorizontalFadingEdgeLength();
8422 return super.getLeftFadingEdgeStrength();
8426 protected float getRightFadingEdgeStrength() {
8427 if (mEllipsize == TextUtils.TruncateAt.MARQUEE &&
8428 mMarqueeFadeMode != MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
8429 if (mMarquee != null && !mMarquee.isStopped()) {
8430 final Marquee marquee = mMarquee;
8431 final float maxFadeScroll = marquee.getMaxFadeScroll();
8432 final float scroll = marquee.getScroll();
8433 return (maxFadeScroll - scroll) / getHorizontalFadingEdgeLength();
8434 } else if (getLineCount() == 1) {
8435 final int layoutDirection = getLayoutDirection();
8436 final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
8437 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
8439 final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() -
8440 getCompoundPaddingRight();
8441 final float lineWidth = mLayout.getLineWidth(0);
8442 return (lineWidth - textWidth) / getHorizontalFadingEdgeLength();
8445 case Gravity.CENTER_HORIZONTAL:
8446 case Gravity.FILL_HORIZONTAL:
8447 final int textDirection = mLayout.getParagraphDirection(0);
8448 if (textDirection == Layout.DIR_RIGHT_TO_LEFT) {
8451 return (mLayout.getLineWidth(0) - ((mRight - mLeft) -
8452 getCompoundPaddingLeft() - getCompoundPaddingRight())) /
8453 getHorizontalFadingEdgeLength();
8458 return super.getRightFadingEdgeStrength();
8462 protected int computeHorizontalScrollRange() {
8463 if (mLayout != null) {
8464 return mSingleLine && (mGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT ?
8465 (int) mLayout.getLineWidth(0) : mLayout.getWidth();
8468 return super.computeHorizontalScrollRange();
8472 protected int computeVerticalScrollRange() {
8473 if (mLayout != null)
8474 return mLayout.getHeight();
8476 return super.computeVerticalScrollRange();
8480 protected int computeVerticalScrollExtent() {
8481 return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom();
8485 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
8486 super.findViewsWithText(outViews, searched, flags);
8487 if (!outViews.contains(this) && (flags & FIND_VIEWS_WITH_TEXT) != 0
8488 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mText)) {
8489 String searchedLowerCase = searched.toString().toLowerCase();
8490 String textLowerCase = mText.toString().toLowerCase();
8491 if (textLowerCase.contains(searchedLowerCase)) {
8497 public enum BufferType {
8498 NORMAL, SPANNABLE, EDITABLE,
8502 * Returns the TextView_textColor attribute from the TypedArray, if set, or
8503 * the TextAppearance_textColor from the TextView_textAppearance attribute,
8504 * if TextView_textColor was not set directly.
8508 public static ColorStateList getTextColors(Context context, TypedArray attrs) {
8509 if (attrs == null) {
8510 // Preserve behavior prior to removal of this API.
8511 throw new NullPointerException();
8514 // It's not safe to use this method from apps. The parameter 'attrs'
8515 // must have been obtained using the TextView filter array which is not
8516 // available to the SDK. As such, we grab a default TypedArray with the
8517 // right filter instead here.
8518 final TypedArray a = context.obtainStyledAttributes(R.styleable.TextView);
8519 ColorStateList colors = a.getColorStateList(R.styleable.TextView_textColor);
8520 if (colors == null) {
8521 final int ap = a.getResourceId(R.styleable.TextView_textAppearance, 0);
8523 final TypedArray appearance = context.obtainStyledAttributes(
8524 ap, R.styleable.TextAppearance);
8525 colors = appearance.getColorStateList(R.styleable.TextAppearance_textColor);
8526 appearance.recycle();
8535 * Returns the default color from the TextView_textColor attribute from the
8536 * AttributeSet, if set, or the default color from the
8537 * TextAppearance_textColor from the TextView_textAppearance attribute, if
8538 * TextView_textColor was not set directly.
8542 public static int getTextColor(Context context, TypedArray attrs, int def) {
8543 final ColorStateList colors = getTextColors(context, attrs);
8544 if (colors == null) {
8547 return colors.getDefaultColor();
8552 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
8553 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
8554 // Handle Ctrl-only shortcuts.
8556 case KeyEvent.KEYCODE_A:
8557 if (canSelectText()) {
8558 return onTextContextMenuItem(ID_SELECT_ALL);
8561 case KeyEvent.KEYCODE_Z:
8563 return onTextContextMenuItem(ID_UNDO);
8566 case KeyEvent.KEYCODE_X:
8568 return onTextContextMenuItem(ID_CUT);
8571 case KeyEvent.KEYCODE_C:
8573 return onTextContextMenuItem(ID_COPY);
8576 case KeyEvent.KEYCODE_V:
8578 return onTextContextMenuItem(ID_PASTE);
8582 } else if (event.hasModifiers(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON)) {
8583 // Handle Ctrl-Shift shortcuts.
8585 case KeyEvent.KEYCODE_Z:
8587 return onTextContextMenuItem(ID_REDO);
8590 case KeyEvent.KEYCODE_V:
8592 return onTextContextMenuItem(ID_PASTE_AS_PLAIN_TEXT);
8596 return super.onKeyShortcut(keyCode, event);
8600 * Unlike {@link #textCanBeSelected()}, this method is based on the <i>current</i> state of the
8601 * TextView. {@link #textCanBeSelected()} has to be true (this is one of the conditions to have
8602 * a selection controller (see {@link Editor#prepareCursorControllers()}), but this is not
8605 boolean canSelectText() {
8606 return mText.length() != 0 && mEditor != null && mEditor.hasSelectionController();
8610 * Test based on the <i>intrinsic</i> charateristics of the TextView.
8611 * The text must be spannable and the movement method must allow for arbitary selection.
8613 * See also {@link #canSelectText()}.
8615 boolean textCanBeSelected() {
8616 // prepareCursorController() relies on this method.
8617 // If you change this condition, make sure prepareCursorController is called anywhere
8618 // the value of this condition might be changed.
8619 if (mMovement == null || !mMovement.canSelectArbitrarily()) return false;
8620 return isTextEditable() ||
8621 (isTextSelectable() && mText instanceof Spannable && isEnabled());
8624 private Locale getTextServicesLocale(boolean allowNullLocale) {
8625 // Start fetching the text services locale asynchronously.
8626 updateTextServicesLocaleAsync();
8627 // If !allowNullLocale and there is no cached text services locale, just return the default
8629 return (mCurrentSpellCheckerLocaleCache == null && !allowNullLocale) ? Locale.getDefault()
8630 : mCurrentSpellCheckerLocaleCache;
8634 * This is a temporary method. Future versions may support multi-locale text.
8635 * Caveat: This method may not return the latest text services locale, but this should be
8636 * acceptable and it's more important to make this method asynchronous.
8638 * @return The locale that should be used for a word iterator
8639 * in this TextView, based on the current spell checker settings,
8640 * the current IME's locale, or the system default locale.
8641 * Please note that a word iterator in this TextView is different from another word iterator
8642 * used by SpellChecker.java of TextView. This method should be used for the former.
8645 // TODO: Support multi-locale
8646 // TODO: Update the text services locale immediately after the keyboard locale is switched
8647 // by catching intent of keyboard switch event
8648 public Locale getTextServicesLocale() {
8649 return getTextServicesLocale(false /* allowNullLocale */);
8653 * @return true if this TextView is specialized for showing and interacting with the extracted
8654 * text in a full-screen input method.
8657 public boolean isInExtractedMode() {
8662 * This is a temporary method. Future versions may support multi-locale text.
8663 * Caveat: This method may not return the latest spell checker locale, but this should be
8664 * acceptable and it's more important to make this method asynchronous.
8666 * @return The locale that should be used for a spell checker in this TextView,
8667 * based on the current spell checker settings, the current IME's locale, or the system default
8671 public Locale getSpellCheckerLocale() {
8672 return getTextServicesLocale(true /* allowNullLocale */);
8675 private void updateTextServicesLocaleAsync() {
8676 // AsyncTask.execute() uses a serial executor which means we don't have
8677 // to lock around updateTextServicesLocaleLocked() to prevent it from
8678 // being executed n times in parallel.
8679 AsyncTask.execute(new Runnable() {
8682 updateTextServicesLocaleLocked();
8687 private void updateTextServicesLocaleLocked() {
8688 final TextServicesManager textServicesManager = (TextServicesManager)
8689 mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
8690 final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true);
8691 final Locale locale;
8692 if (subtype != null) {
8693 locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale());
8697 mCurrentSpellCheckerLocaleCache = locale;
8700 void onLocaleChanged() {
8701 // Will be re-created on demand in getWordIterator with the proper new locale
8702 mEditor.mWordIterator = null;
8706 * This method is used by the ArrowKeyMovementMethod to jump from one word to the other.
8707 * Made available to achieve a consistent behavior.
8710 public WordIterator getWordIterator() {
8711 if (mEditor != null) {
8712 return mEditor.getWordIterator();
8720 public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
8721 super.onPopulateAccessibilityEventInternal(event);
8723 final boolean isPassword = hasPasswordTransformationMethod();
8724 if (!isPassword || shouldSpeakPasswordsForAccessibility()) {
8725 final CharSequence text = getTextForAccessibility();
8726 if (!TextUtils.isEmpty(text)) {
8727 event.getText().add(text);
8733 * @return true if the user has explicitly allowed accessibility services
8734 * to speak passwords.
8736 private boolean shouldSpeakPasswordsForAccessibility() {
8737 return (Settings.Secure.getIntForUser(mContext.getContentResolver(),
8738 Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0,
8739 UserHandle.USER_CURRENT_OR_SELF) == 1);
8743 public CharSequence getAccessibilityClassName() {
8744 return TextView.class.getName();
8748 public void onProvideStructure(ViewStructure structure) {
8749 super.onProvideStructure(structure);
8750 final boolean isPassword = hasPasswordTransformationMethod()
8751 || isPasswordInputType(getInputType());
8753 structure.setText(getText(), getSelectionStart(), getSelectionEnd());
8755 // Extract style information that applies to the TextView as a whole.
8757 int typefaceStyle = getTypefaceStyle();
8758 if ((typefaceStyle & Typeface.BOLD) != 0) {
8759 style |= AssistStructure.ViewNode.TEXT_STYLE_BOLD;
8761 if ((typefaceStyle & Typeface.ITALIC) != 0) {
8762 style |= AssistStructure.ViewNode.TEXT_STYLE_ITALIC;
8765 // Global styles can also be set via TextView.setPaintFlags().
8766 int paintFlags = mTextPaint.getFlags();
8767 if ((paintFlags & Paint.FAKE_BOLD_TEXT_FLAG) != 0) {
8768 style |= AssistStructure.ViewNode.TEXT_STYLE_BOLD;
8770 if ((paintFlags & Paint.UNDERLINE_TEXT_FLAG) != 0) {
8771 style |= AssistStructure.ViewNode.TEXT_STYLE_UNDERLINE;
8773 if ((paintFlags & Paint.STRIKE_THRU_TEXT_FLAG) != 0) {
8774 style |= AssistStructure.ViewNode.TEXT_STYLE_STRIKE_THRU;
8777 // TextView does not have its own text background color. A background is either part
8778 // of the View (and can be any drawable) or a BackgroundColorSpan inside the text.
8779 structure.setTextStyle(getTextSize(), getCurrentTextColor(),
8780 AssistStructure.ViewNode.TEXT_COLOR_UNDEFINED /* bgColor */, style);
8782 structure.setHint(getHint());
8787 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
8788 super.onInitializeAccessibilityEventInternal(event);
8790 final boolean isPassword = hasPasswordTransformationMethod();
8791 event.setPassword(isPassword);
8793 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) {
8794 event.setFromIndex(Selection.getSelectionStart(mText));
8795 event.setToIndex(Selection.getSelectionEnd(mText));
8796 event.setItemCount(mText.length());
8802 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
8803 super.onInitializeAccessibilityNodeInfoInternal(info);
8805 final boolean isPassword = hasPasswordTransformationMethod();
8806 info.setPassword(isPassword);
8808 if (!isPassword || shouldSpeakPasswordsForAccessibility()) {
8809 info.setText(getTextForAccessibility());
8812 if (mBufferType == BufferType.EDITABLE) {
8813 info.setEditable(true);
8816 if (mEditor != null) {
8817 info.setInputType(mEditor.mInputType);
8819 if (mEditor.mError != null) {
8820 info.setContentInvalid(true);
8821 info.setError(mEditor.mError);
8825 if (!TextUtils.isEmpty(mText)) {
8826 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
8827 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
8828 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
8829 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
8830 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE
8831 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH
8832 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
8836 if (canSelectText()) {
8837 info.addAction(AccessibilityNodeInfo.ACTION_SET_SELECTION);
8840 info.addAction(AccessibilityNodeInfo.ACTION_COPY);
8843 info.addAction(AccessibilityNodeInfo.ACTION_PASTE);
8846 info.addAction(AccessibilityNodeInfo.ACTION_CUT);
8850 // Check for known input filter types.
8851 final int numFilters = mFilters.length;
8852 for (int i = 0; i < numFilters; i++) {
8853 final InputFilter filter = mFilters[i];
8854 if (filter instanceof InputFilter.LengthFilter) {
8855 info.setMaxTextLength(((InputFilter.LengthFilter) filter).getMax());
8859 if (!isSingleLine()) {
8860 info.setMultiLine(true);
8865 * Performs an accessibility action after it has been offered to the
8871 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
8873 case AccessibilityNodeInfo.ACTION_CLICK: {
8874 boolean handled = false;
8876 // Simulate View.onTouchEvent for an ACTION_UP event.
8877 if (isClickable() || isLongClickable()) {
8878 if (isFocusable() && !isFocused()) {
8886 // Simulate TextView.onTouchEvent for an ACTION_UP event.
8887 if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
8888 && mText instanceof Spannable && mLayout != null
8889 && (isTextEditable() || isTextSelectable()) && isFocused()) {
8890 // Show the IME, except when selecting in read-only text.
8891 final InputMethodManager imm = InputMethodManager.peekInstance();
8893 if (!isTextSelectable() && mEditor.mShowSoftInputOnFocus && imm != null) {
8894 handled |= imm.showSoftInput(this, 0);
8900 case AccessibilityNodeInfo.ACTION_COPY: {
8901 if (isFocused() && canCopy()) {
8902 if (onTextContextMenuItem(ID_COPY)) {
8907 case AccessibilityNodeInfo.ACTION_PASTE: {
8908 if (isFocused() && canPaste()) {
8909 if (onTextContextMenuItem(ID_PASTE)) {
8914 case AccessibilityNodeInfo.ACTION_CUT: {
8915 if (isFocused() && canCut()) {
8916 if (onTextContextMenuItem(ID_CUT)) {
8921 case AccessibilityNodeInfo.ACTION_SET_SELECTION: {
8922 if (isFocused() && canSelectText()) {
8923 ensureIterableTextForAccessibilitySelectable();
8924 CharSequence text = getIterableTextForAccessibility();
8928 final int start = (arguments != null) ? arguments.getInt(
8929 AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, -1) : -1;
8930 final int end = (arguments != null) ? arguments.getInt(
8931 AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, -1) : -1;
8932 if ((getSelectionStart() != start || getSelectionEnd() != end)) {
8933 // No arguments clears the selection.
8934 if (start == end && end == -1) {
8935 Selection.removeSelection((Spannable) text);
8938 if (start >= 0 && start <= end && end <= text.length()) {
8939 Selection.setSelection((Spannable) text, start, end);
8940 // Make sure selection mode is engaged.
8941 if (mEditor != null) {
8942 mEditor.startSelectionActionMode();
8949 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY:
8950 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
8951 ensureIterableTextForAccessibilitySelectable();
8952 return super.performAccessibilityActionInternal(action, arguments);
8955 return super.performAccessibilityActionInternal(action, arguments);
8962 public void sendAccessibilityEventInternal(int eventType) {
8963 // Do not send scroll events since first they are not interesting for
8964 // accessibility and second such events a generated too frequently.
8965 // For details see the implementation of bringTextIntoView().
8966 if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
8969 super.sendAccessibilityEventInternal(eventType);
8973 * Gets the text reported for accessibility purposes.
8975 * @return The accessibility text.
8979 public CharSequence getTextForAccessibility() {
8980 CharSequence text = getText();
8981 if (TextUtils.isEmpty(text)) {
8987 void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText,
8988 int fromIndex, int removedCount, int addedCount) {
8989 AccessibilityEvent event =
8990 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
8991 event.setFromIndex(fromIndex);
8992 event.setRemovedCount(removedCount);
8993 event.setAddedCount(addedCount);
8994 event.setBeforeText(beforeText);
8995 sendAccessibilityEventUnchecked(event);
8999 * Returns whether this text view is a current input method target. The
9000 * default implementation just checks with {@link InputMethodManager}.
9002 public boolean isInputMethodTarget() {
9003 InputMethodManager imm = InputMethodManager.peekInstance();
9004 return imm != null && imm.isActive(this);
9007 static final int ID_SELECT_ALL = android.R.id.selectAll;
9008 static final int ID_UNDO = android.R.id.undo;
9009 static final int ID_REDO = android.R.id.redo;
9010 static final int ID_CUT = android.R.id.cut;
9011 static final int ID_COPY = android.R.id.copy;
9012 static final int ID_PASTE = android.R.id.paste;
9013 static final int ID_SHARE = android.R.id.shareText;
9014 static final int ID_PASTE_AS_PLAIN_TEXT = android.R.id.pasteAsPlainText;
9015 static final int ID_REPLACE = android.R.id.replaceText;
9018 * Called when a context menu option for the text view is selected. Currently
9019 * this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut},
9020 * {@link android.R.id#copy}, {@link android.R.id#paste} or {@link android.R.id#shareText}.
9022 * @return true if the context menu item action was performed.
9024 public boolean onTextContextMenuItem(int id) {
9026 int max = mText.length();
9029 final int selStart = getSelectionStart();
9030 final int selEnd = getSelectionEnd();
9032 min = Math.max(0, Math.min(selStart, selEnd));
9033 max = Math.max(0, Math.max(selStart, selEnd));
9038 // This starts an action mode if triggered from another action mode. Text is
9039 // highlighted, so that it can be bulk edited, like selectAllOnFocus does. Returns
9040 // true even if text is empty.
9041 boolean shouldRestartActionMode =
9042 mEditor != null && mEditor.mTextActionMode != null;
9043 stopTextActionMode();
9045 if (shouldRestartActionMode) {
9046 mEditor.startSelectionActionMode();
9051 if (mEditor != null) {
9054 return true; // Returns true even if nothing was undone.
9057 if (mEditor != null) {
9060 return true; // Returns true even if nothing was undone.
9063 paste(min, max, true /* withFormatting */);
9066 case ID_PASTE_AS_PLAIN_TEXT:
9067 paste(min, max, false /* withFormatting */);
9071 setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
9072 deleteText_internal(min, max);
9073 stopTextActionMode();
9077 setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
9078 stopTextActionMode();
9082 if (mEditor != null) {
9088 shareSelectedText();
9094 CharSequence getTransformedText(int start, int end) {
9095 return removeSuggestionSpans(mTransformed.subSequence(start, end));
9099 public boolean performLongClick() {
9100 boolean handled = false;
9102 if (super.performLongClick()) {
9106 if (mEditor != null) {
9107 handled |= mEditor.performLongClick(handled);
9111 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
9112 if (mEditor != null) mEditor.mDiscardNextActionUp = true;
9119 protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
9120 super.onScrollChanged(horiz, vert, oldHoriz, oldVert);
9121 if (mEditor != null) {
9122 mEditor.onScrollChanged();
9127 * Return whether or not suggestions are enabled on this TextView. The suggestions are generated
9128 * by the IME or by the spell checker as the user types. This is done by adding
9129 * {@link SuggestionSpan}s to the text.
9131 * When suggestions are enabled (default), this list of suggestions will be displayed when the
9132 * user asks for them on these parts of the text. This value depends on the inputType of this
9135 * The class of the input type must be {@link InputType#TYPE_CLASS_TEXT}.
9137 * In addition, the type variation must be one of
9138 * {@link InputType#TYPE_TEXT_VARIATION_NORMAL},
9139 * {@link InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT},
9140 * {@link InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE},
9141 * {@link InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE} or
9142 * {@link InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}.
9144 * And finally, the {@link InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS} flag must <i>not</i> be set.
9146 * @return true if the suggestions popup window is enabled, based on the inputType.
9148 public boolean isSuggestionsEnabled() {
9149 if (mEditor == null) return false;
9150 if ((mEditor.mInputType & InputType.TYPE_MASK_CLASS) != InputType.TYPE_CLASS_TEXT) {
9153 if ((mEditor.mInputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) > 0) return false;
9155 final int variation = mEditor.mInputType & EditorInfo.TYPE_MASK_VARIATION;
9156 return (variation == EditorInfo.TYPE_TEXT_VARIATION_NORMAL ||
9157 variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT ||
9158 variation == EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE ||
9159 variation == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE ||
9160 variation == EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
9164 * If provided, this ActionMode.Callback will be used to create the ActionMode when text
9165 * selection is initiated in this View.
9167 * The standard implementation populates the menu with a subset of Select All, Cut, Copy,
9168 * Paste, Replace and Share actions, depending on what this View supports.
9170 * A custom implementation can add new entries in the default menu in its
9171 * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The
9172 * default actions can also be removed from the menu using
9173 * {@link android.view.Menu#removeItem(int)} and passing {@link android.R.id#selectAll},
9174 * {@link android.R.id#cut}, {@link android.R.id#copy}, {@link android.R.id#paste},
9175 * {@link android.R.id#replaceText} or {@link android.R.id#shareText} ids as parameters.
9177 * Returning false from
9178 * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent
9179 * the action mode from being started.
9181 * Action click events should be handled by the custom implementation of
9182 * {@link android.view.ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}.
9184 * Note that text selection mode is not started when a TextView receives focus and the
9185 * {@link android.R.attr#selectAllOnFocus} flag has been set. The content is highlighted in
9186 * that case, to allow for quick replacement.
9188 public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {
9189 createEditorIfNeeded();
9190 mEditor.mCustomSelectionActionModeCallback = actionModeCallback;
9194 * Retrieves the value set in {@link #setCustomSelectionActionModeCallback}. Default is null.
9196 * @return The current custom selection callback.
9198 public ActionMode.Callback getCustomSelectionActionModeCallback() {
9199 return mEditor == null ? null : mEditor.mCustomSelectionActionModeCallback;
9203 * If provided, this ActionMode.Callback will be used to create the ActionMode when text
9204 * insertion is initiated in this View.
9205 * The standard implementation populates the menu with a subset of Select All,
9206 * Paste and Replace actions, depending on what this View supports.
9208 * <p>A custom implementation can add new entries in the default menu in its
9209 * {@link android.view.ActionMode.Callback#onPrepareActionMode(android.view.ActionMode,
9210 * android.view.Menu)} method. The default actions can also be removed from the menu using
9211 * {@link android.view.Menu#removeItem(int)} and passing {@link android.R.id#selectAll},
9212 * {@link android.R.id#paste} or {@link android.R.id#replaceText} ids as parameters.</p>
9214 * <p>Returning false from
9215 * {@link android.view.ActionMode.Callback#onCreateActionMode(android.view.ActionMode,
9216 * android.view.Menu)} will prevent the action mode from being started.</p>
9218 * <p>Action click events should be handled by the custom implementation of
9219 * {@link android.view.ActionMode.Callback#onActionItemClicked(android.view.ActionMode,
9220 * android.view.MenuItem)}.</p>
9222 * <p>Note that text insertion mode is not started when a TextView receives focus and the
9223 * {@link android.R.attr#selectAllOnFocus} flag has been set.</p>
9225 public void setCustomInsertionActionModeCallback(ActionMode.Callback actionModeCallback) {
9226 createEditorIfNeeded();
9227 mEditor.mCustomInsertionActionModeCallback = actionModeCallback;
9231 * Retrieves the value set in {@link #setCustomInsertionActionModeCallback}. Default is null.
9233 * @return The current custom insertion callback.
9235 public ActionMode.Callback getCustomInsertionActionModeCallback() {
9236 return mEditor == null ? null : mEditor.mCustomInsertionActionModeCallback;
9242 protected void stopTextActionMode() {
9243 if (mEditor != null) {
9244 mEditor.stopTextActionMode();
9249 return mEditor != null && mEditor.canUndo();
9253 return mEditor != null && mEditor.canRedo();
9257 if (hasPasswordTransformationMethod()) {
9261 if (mText.length() > 0 && hasSelection() && mText instanceof Editable && mEditor != null &&
9262 mEditor.mKeyListener != null) {
9270 if (hasPasswordTransformationMethod()) {
9274 if (mText.length() > 0 && hasSelection() && mEditor != null) {
9281 boolean canShare() {
9285 boolean canPaste() {
9286 return (mText instanceof Editable &&
9287 mEditor != null && mEditor.mKeyListener != null &&
9288 getSelectionStart() >= 0 &&
9289 getSelectionEnd() >= 0 &&
9290 ((ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE)).
9294 boolean canProcessText() {
9295 if (!getContext().canStartActivityForResult() || getId() == View.NO_ID
9296 || hasPasswordTransformationMethod()) {
9300 if (mText.length() > 0 && hasSelection() && mEditor != null) {
9307 boolean canSelectAllText() {
9308 return canSelectText() && !hasPasswordTransformationMethod()
9309 && !(getSelectionStart() == 0 && getSelectionEnd() == mText.length());
9312 boolean selectAllText() {
9313 // Need to hide insert point cursor controller before settings selection, otherwise insert
9314 // point cursor controller obtains cursor update event and update cursor with cancelling
9316 if (mEditor != null) {
9317 mEditor.hideInsertionPointCursorController();
9319 final int length = mText.length();
9320 Selection.setSelection((Spannable) mText, 0, length);
9324 void replaceSelectionWithText(CharSequence text) {
9325 ((Editable) mText).replace(getSelectionStart(), getSelectionEnd(), text);
9329 * Paste clipboard content between min and max positions.
9331 private void paste(int min, int max, boolean withFormatting) {
9332 ClipboardManager clipboard =
9333 (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
9334 ClipData clip = clipboard.getPrimaryClip();
9336 boolean didFirst = false;
9337 for (int i=0; i<clip.getItemCount(); i++) {
9338 final CharSequence paste;
9339 if (withFormatting) {
9340 paste = clip.getItemAt(i).coerceToStyledText(getContext());
9342 // Get an item as text and remove all spans by toString().
9343 final CharSequence text = clip.getItemAt(i).coerceToText(getContext());
9344 paste = (text instanceof Spanned) ? text.toString() : text;
9346 if (paste != null) {
9348 Selection.setSelection((Spannable) mText, max);
9349 ((Editable) mText).replace(min, max, paste);
9352 ((Editable) mText).insert(getSelectionEnd(), "\n");
9353 ((Editable) mText).insert(getSelectionEnd(), paste);
9357 stopTextActionMode();
9358 sLastCutCopyOrTextChangedTime = 0;
9362 private void shareSelectedText() {
9363 String selectedText = getSelectedText();
9364 if (selectedText != null && !selectedText.isEmpty()) {
9365 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
9366 sharingIntent.setType("text/plain");
9367 sharingIntent.removeExtra(android.content.Intent.EXTRA_TEXT);
9368 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, selectedText);
9369 getContext().startActivity(Intent.createChooser(sharingIntent, null));
9370 stopTextActionMode();
9374 private void setPrimaryClip(ClipData clip) {
9375 ClipboardManager clipboard = (ClipboardManager) getContext().
9376 getSystemService(Context.CLIPBOARD_SERVICE);
9377 clipboard.setPrimaryClip(clip);
9378 sLastCutCopyOrTextChangedTime = SystemClock.uptimeMillis();
9382 * Get the character offset closest to the specified absolute position. A typical use case is to
9383 * pass the result of {@link MotionEvent#getX()} and {@link MotionEvent#getY()} to this method.
9385 * @param x The horizontal absolute position of a point on screen
9386 * @param y The vertical absolute position of a point on screen
9387 * @return the character offset for the character whose position is closest to the specified
9388 * position. Returns -1 if there is no layout.
9390 public int getOffsetForPosition(float x, float y) {
9391 if (getLayout() == null) return -1;
9392 final int line = getLineAtCoordinate(y);
9393 final int offset = getOffsetAtCoordinate(line, x);
9397 float convertToLocalHorizontalCoordinate(float x) {
9398 x -= getTotalPaddingLeft();
9399 // Clamp the position to inside of the view.
9400 x = Math.max(0.0f, x);
9401 x = Math.min(getWidth() - getTotalPaddingRight() - 1, x);
9406 int getLineAtCoordinate(float y) {
9407 y -= getTotalPaddingTop();
9408 // Clamp the position to inside of the view.
9409 y = Math.max(0.0f, y);
9410 y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y);
9412 return getLayout().getLineForVertical((int) y);
9415 int getOffsetAtCoordinate(int line, float x) {
9416 x = convertToLocalHorizontalCoordinate(x);
9417 return getLayout().getOffsetForHorizontal(line, x);
9421 public boolean onDragEvent(DragEvent event) {
9422 switch (event.getAction()) {
9423 case DragEvent.ACTION_DRAG_STARTED:
9424 return mEditor != null && mEditor.hasInsertionController();
9426 case DragEvent.ACTION_DRAG_ENTERED:
9427 TextView.this.requestFocus();
9430 case DragEvent.ACTION_DRAG_LOCATION:
9431 final int offset = getOffsetForPosition(event.getX(), event.getY());
9432 Selection.setSelection((Spannable)mText, offset);
9435 case DragEvent.ACTION_DROP:
9436 if (mEditor != null) mEditor.onDrop(event);
9439 case DragEvent.ACTION_DRAG_ENDED:
9440 case DragEvent.ACTION_DRAG_EXITED:
9446 boolean isInBatchEditMode() {
9447 if (mEditor == null) return false;
9448 final Editor.InputMethodState ims = mEditor.mInputMethodState;
9450 return ims.mBatchEditNesting > 0;
9452 return mEditor.mInBatchEditControllers;
9456 public void onRtlPropertiesChanged(int layoutDirection) {
9457 super.onRtlPropertiesChanged(layoutDirection);
9459 final TextDirectionHeuristic newTextDir = getTextDirectionHeuristic();
9460 if (mTextDir != newTextDir) {
9461 mTextDir = newTextDir;
9462 if (mLayout != null) {
9468 TextDirectionHeuristic getTextDirectionHeuristic() {
9469 if (hasPasswordTransformationMethod()) {
9470 // passwords fields should be LTR
9471 return TextDirectionHeuristics.LTR;
9474 // Always need to resolve layout direction first
9475 final boolean defaultIsRtl = (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
9477 // Now, we can select the heuristic
9478 switch (getTextDirection()) {
9480 case TEXT_DIRECTION_FIRST_STRONG:
9481 return (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL :
9482 TextDirectionHeuristics.FIRSTSTRONG_LTR);
9483 case TEXT_DIRECTION_ANY_RTL:
9484 return TextDirectionHeuristics.ANYRTL_LTR;
9485 case TEXT_DIRECTION_LTR:
9486 return TextDirectionHeuristics.LTR;
9487 case TEXT_DIRECTION_RTL:
9488 return TextDirectionHeuristics.RTL;
9489 case TEXT_DIRECTION_LOCALE:
9490 return TextDirectionHeuristics.LOCALE;
9491 case TEXT_DIRECTION_FIRST_STRONG_LTR:
9492 return TextDirectionHeuristics.FIRSTSTRONG_LTR;
9493 case TEXT_DIRECTION_FIRST_STRONG_RTL:
9494 return TextDirectionHeuristics.FIRSTSTRONG_RTL;
9502 public void onResolveDrawables(int layoutDirection) {
9503 // No need to resolve twice
9504 if (mLastLayoutDirection == layoutDirection) {
9507 mLastLayoutDirection = layoutDirection;
9509 // Resolve drawables
9510 if (mDrawables != null) {
9511 mDrawables.resolveWithLayoutDirection(layoutDirection);
9518 protected void resetResolvedDrawables() {
9519 super.resetResolvedDrawables();
9520 mLastLayoutDirection = -1;
9526 protected void viewClicked(InputMethodManager imm) {
9528 imm.viewClicked(this);
9533 * Deletes the range of text [start, end[.
9536 protected void deleteText_internal(int start, int end) {
9537 ((Editable) mText).delete(start, end);
9541 * Replaces the range of text [start, end[ by replacement text
9544 protected void replaceText_internal(int start, int end, CharSequence text) {
9545 ((Editable) mText).replace(start, end, text);
9549 * Sets a span on the specified range of text
9552 protected void setSpan_internal(Object span, int start, int end, int flags) {
9553 ((Editable) mText).setSpan(span, start, end, flags);
9557 * Moves the cursor to the specified offset position in text
9560 protected void setCursorPosition_internal(int start, int end) {
9561 Selection.setSelection(((Editable) mText), start, end);
9565 * An Editor should be created as soon as any of the editable-specific fields (grouped
9566 * inside the Editor object) is assigned to a non-default value.
9567 * This method will create the Editor if needed.
9569 * A standard TextView (as well as buttons, checkboxes...) should not qualify and hence will
9570 * have a null Editor, unlike an EditText. Inconsistent in-between states will have an
9571 * Editor for backward compatibility, as soon as one of these fields is assigned.
9573 * Also note that for performance reasons, the mEditor is created when needed, but not
9574 * reset when no more edit-specific fields are needed.
9576 private void createEditorIfNeeded() {
9577 if (mEditor == null) {
9578 mEditor = new Editor(this);
9586 public CharSequence getIterableTextForAccessibility() {
9590 private void ensureIterableTextForAccessibilitySelectable() {
9591 if (!(mText instanceof Spannable)) {
9592 setText(mText, BufferType.SPANNABLE);
9600 public TextSegmentIterator getIteratorForGranularity(int granularity) {
9601 switch (granularity) {
9602 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE: {
9603 Spannable text = (Spannable) getIterableTextForAccessibility();
9604 if (!TextUtils.isEmpty(text) && getLayout() != null) {
9605 AccessibilityIterators.LineTextSegmentIterator iterator =
9606 AccessibilityIterators.LineTextSegmentIterator.getInstance();
9607 iterator.initialize(text, getLayout());
9611 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE: {
9612 Spannable text = (Spannable) getIterableTextForAccessibility();
9613 if (!TextUtils.isEmpty(text) && getLayout() != null) {
9614 AccessibilityIterators.PageTextSegmentIterator iterator =
9615 AccessibilityIterators.PageTextSegmentIterator.getInstance();
9616 iterator.initialize(this);
9621 return super.getIteratorForGranularity(granularity);
9628 public int getAccessibilitySelectionStart() {
9629 return getSelectionStart();
9635 public boolean isAccessibilitySelectionExtendable() {
9643 public int getAccessibilitySelectionEnd() {
9644 return getSelectionEnd();
9651 public void setAccessibilitySelection(int start, int end) {
9652 if (getAccessibilitySelectionStart() == start
9653 && getAccessibilitySelectionEnd() == end) {
9656 // Hide all selection controllers used for adjusting selection
9657 // since we are doing so explicitlty by other means and these
9658 // controllers interact with how selection behaves.
9659 if (mEditor != null) {
9660 mEditor.hideCursorAndSpanControllers();
9662 CharSequence text = getIterableTextForAccessibility();
9663 if (Math.min(start, end) >= 0 && Math.max(start, end) <= text.length()) {
9664 Selection.setSelection((Spannable) text, start, end);
9666 Selection.removeSelection((Spannable) text);
9672 protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) {
9673 super.encodeProperties(stream);
9675 TruncateAt ellipsize = getEllipsize();
9676 stream.addProperty("text:ellipsize", ellipsize == null ? null : ellipsize.name());
9677 stream.addProperty("text:textSize", getTextSize());
9678 stream.addProperty("text:scaledTextSize", getScaledTextSize());
9679 stream.addProperty("text:typefaceStyle", getTypefaceStyle());
9680 stream.addProperty("text:selectionStart", getSelectionStart());
9681 stream.addProperty("text:selectionEnd", getSelectionEnd());
9682 stream.addProperty("text:curTextColor", mCurTextColor);
9683 stream.addProperty("text:text", mText == null ? null : mText.toString());
9684 stream.addProperty("text:gravity", mGravity);
9688 * User interface state that is stored by TextView for implementing
9689 * {@link View#onSaveInstanceState}.
9691 public static class SavedState extends BaseSavedState {
9695 boolean frozenWithFocus;
9697 ParcelableParcel editorState; // Optional state from Editor.
9699 SavedState(Parcelable superState) {
9704 public void writeToParcel(Parcel out, int flags) {
9705 super.writeToParcel(out, flags);
9706 out.writeInt(selStart);
9707 out.writeInt(selEnd);
9708 out.writeInt(frozenWithFocus ? 1 : 0);
9709 TextUtils.writeToParcel(text, out, flags);
9711 if (error == null) {
9715 TextUtils.writeToParcel(error, out, flags);
9718 if (editorState == null) {
9722 editorState.writeToParcel(out, flags);
9727 public String toString() {
9728 String str = "TextView.SavedState{"
9729 + Integer.toHexString(System.identityHashCode(this))
9730 + " start=" + selStart + " end=" + selEnd;
9732 str += " text=" + text;
9737 @SuppressWarnings("hiding")
9738 public static final Parcelable.Creator<SavedState> CREATOR
9739 = new Parcelable.Creator<SavedState>() {
9740 public SavedState createFromParcel(Parcel in) {
9741 return new SavedState(in);
9744 public SavedState[] newArray(int size) {
9745 return new SavedState[size];
9749 private SavedState(Parcel in) {
9751 selStart = in.readInt();
9752 selEnd = in.readInt();
9753 frozenWithFocus = (in.readInt() != 0);
9754 text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
9756 if (in.readInt() != 0) {
9757 error = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
9760 if (in.readInt() != 0) {
9761 editorState = ParcelableParcel.CREATOR.createFromParcel(in);
9766 private static class CharWrapper implements CharSequence, GetChars, GraphicsOperations {
9767 private char[] mChars;
9768 private int mStart, mLength;
9770 public CharWrapper(char[] chars, int start, int len) {
9776 /* package */ void set(char[] chars, int start, int len) {
9782 public int length() {
9786 public char charAt(int off) {
9787 return mChars[off + mStart];
9791 public String toString() {
9792 return new String(mChars, mStart, mLength);
9795 public CharSequence subSequence(int start, int end) {
9796 if (start < 0 || end < 0 || start > mLength || end > mLength) {
9797 throw new IndexOutOfBoundsException(start + ", " + end);
9800 return new String(mChars, start + mStart, end - start);
9803 public void getChars(int start, int end, char[] buf, int off) {
9804 if (start < 0 || end < 0 || start > mLength || end > mLength) {
9805 throw new IndexOutOfBoundsException(start + ", " + end);
9808 System.arraycopy(mChars, start + mStart, buf, off, end - start);
9811 public void drawText(Canvas c, int start, int end,
9812 float x, float y, Paint p) {
9813 c.drawText(mChars, start + mStart, end - start, x, y, p);
9816 public void drawTextRun(Canvas c, int start, int end,
9817 int contextStart, int contextEnd, float x, float y, boolean isRtl, Paint p) {
9818 int count = end - start;
9819 int contextCount = contextEnd - contextStart;
9820 c.drawTextRun(mChars, start + mStart, count, contextStart + mStart,
9821 contextCount, x, y, isRtl, p);
9824 public float measureText(int start, int end, Paint p) {
9825 return p.measureText(mChars, start + mStart, end - start);
9828 public int getTextWidths(int start, int end, float[] widths, Paint p) {
9829 return p.getTextWidths(mChars, start + mStart, end - start, widths);
9832 public float getTextRunAdvances(int start, int end, int contextStart,
9833 int contextEnd, boolean isRtl, float[] advances, int advancesIndex,
9835 int count = end - start;
9836 int contextCount = contextEnd - contextStart;
9837 return p.getTextRunAdvances(mChars, start + mStart, count,
9838 contextStart + mStart, contextCount, isRtl, advances,
9842 public int getTextRunCursor(int contextStart, int contextEnd, int dir,
9843 int offset, int cursorOpt, Paint p) {
9844 int contextCount = contextEnd - contextStart;
9845 return p.getTextRunCursor(mChars, contextStart + mStart,
9846 contextCount, dir, offset + mStart, cursorOpt);
9850 private static final class Marquee {
9851 // TODO: Add an option to configure this
9852 private static final float MARQUEE_DELTA_MAX = 0.07f;
9853 private static final int MARQUEE_DELAY = 1200;
9854 private static final int MARQUEE_DP_PER_SECOND = 30;
9856 private static final byte MARQUEE_STOPPED = 0x0;
9857 private static final byte MARQUEE_STARTING = 0x1;
9858 private static final byte MARQUEE_RUNNING = 0x2;
9860 private final WeakReference<TextView> mView;
9861 private final Choreographer mChoreographer;
9863 private byte mStatus = MARQUEE_STOPPED;
9864 private final float mPixelsPerSecond;
9865 private float mMaxScroll;
9866 private float mMaxFadeScroll;
9867 private float mGhostStart;
9868 private float mGhostOffset;
9869 private float mFadeStop;
9870 private int mRepeatLimit;
9872 private float mScroll;
9873 private long mLastAnimationMs;
9875 Marquee(TextView v) {
9876 final float density = v.getContext().getResources().getDisplayMetrics().density;
9877 mPixelsPerSecond = MARQUEE_DP_PER_SECOND * density;
9878 mView = new WeakReference<TextView>(v);
9879 mChoreographer = Choreographer.getInstance();
9882 private Choreographer.FrameCallback mTickCallback = new Choreographer.FrameCallback() {
9884 public void doFrame(long frameTimeNanos) {
9889 private Choreographer.FrameCallback mStartCallback = new Choreographer.FrameCallback() {
9891 public void doFrame(long frameTimeNanos) {
9892 mStatus = MARQUEE_RUNNING;
9893 mLastAnimationMs = mChoreographer.getFrameTime();
9898 private Choreographer.FrameCallback mRestartCallback = new Choreographer.FrameCallback() {
9900 public void doFrame(long frameTimeNanos) {
9901 if (mStatus == MARQUEE_RUNNING) {
9902 if (mRepeatLimit >= 0) {
9905 start(mRepeatLimit);
9911 if (mStatus != MARQUEE_RUNNING) {
9915 mChoreographer.removeFrameCallback(mTickCallback);
9917 final TextView textView = mView.get();
9918 if (textView != null && (textView.isFocused() || textView.isSelected())) {
9919 long currentMs = mChoreographer.getFrameTime();
9920 long deltaMs = currentMs - mLastAnimationMs;
9921 mLastAnimationMs = currentMs;
9922 float deltaPx = deltaMs / 1000f * mPixelsPerSecond;
9924 if (mScroll > mMaxScroll) {
9925 mScroll = mMaxScroll;
9926 mChoreographer.postFrameCallbackDelayed(mRestartCallback, MARQUEE_DELAY);
9928 mChoreographer.postFrameCallback(mTickCallback);
9930 textView.invalidate();
9935 mStatus = MARQUEE_STOPPED;
9936 mChoreographer.removeFrameCallback(mStartCallback);
9937 mChoreographer.removeFrameCallback(mRestartCallback);
9938 mChoreographer.removeFrameCallback(mTickCallback);
9942 private void resetScroll() {
9944 final TextView textView = mView.get();
9945 if (textView != null) textView.invalidate();
9948 void start(int repeatLimit) {
9949 if (repeatLimit == 0) {
9953 mRepeatLimit = repeatLimit;
9954 final TextView textView = mView.get();
9955 if (textView != null && textView.mLayout != null) {
9956 mStatus = MARQUEE_STARTING;
9958 final int textWidth = textView.getWidth() - textView.getCompoundPaddingLeft() -
9959 textView.getCompoundPaddingRight();
9960 final float lineWidth = textView.mLayout.getLineWidth(0);
9961 final float gap = textWidth / 3.0f;
9962 mGhostStart = lineWidth - textWidth + gap;
9963 mMaxScroll = mGhostStart + textWidth;
9964 mGhostOffset = lineWidth + gap;
9965 mFadeStop = lineWidth + textWidth / 6.0f;
9966 mMaxFadeScroll = mGhostStart + lineWidth + lineWidth;
9968 textView.invalidate();
9969 mChoreographer.postFrameCallback(mStartCallback);
9973 float getGhostOffset() {
9974 return mGhostOffset;
9981 float getMaxFadeScroll() {
9982 return mMaxFadeScroll;
9985 boolean shouldDrawLeftFade() {
9986 return mScroll <= mFadeStop;
9989 boolean shouldDrawGhost() {
9990 return mStatus == MARQUEE_RUNNING && mScroll > mGhostStart;
9993 boolean isRunning() {
9994 return mStatus == MARQUEE_RUNNING;
9997 boolean isStopped() {
9998 return mStatus == MARQUEE_STOPPED;
10002 private class ChangeWatcher implements TextWatcher, SpanWatcher {
10004 private CharSequence mBeforeText;
10006 public void beforeTextChanged(CharSequence buffer, int start,
10007 int before, int after) {
10008 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "beforeTextChanged start=" + start
10009 + " before=" + before + " after=" + after + ": " + buffer);
10011 if (AccessibilityManager.getInstance(mContext).isEnabled()
10012 && ((!isPasswordInputType(getInputType()) && !hasPasswordTransformationMethod())
10013 || shouldSpeakPasswordsForAccessibility())) {
10014 mBeforeText = buffer.toString();
10017 TextView.this.sendBeforeTextChanged(buffer, start, before, after);
10020 public void onTextChanged(CharSequence buffer, int start, int before, int after) {
10021 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onTextChanged start=" + start
10022 + " before=" + before + " after=" + after + ": " + buffer);
10023 TextView.this.handleTextChanged(buffer, start, before, after);
10025 if (AccessibilityManager.getInstance(mContext).isEnabled() &&
10026 (isFocused() || isSelected() && isShown())) {
10027 sendAccessibilityEventTypeViewTextChanged(mBeforeText, start, before, after);
10028 mBeforeText = null;
10032 public void afterTextChanged(Editable buffer) {
10033 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "afterTextChanged: " + buffer);
10034 TextView.this.sendAfterTextChanged(buffer);
10036 if (MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0) {
10037 MetaKeyKeyListener.stopSelecting(TextView.this, buffer);
10041 public void onSpanChanged(Spannable buf, Object what, int s, int e, int st, int en) {
10042 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanChanged s=" + s + " e=" + e
10043 + " st=" + st + " en=" + en + " what=" + what + ": " + buf);
10044 TextView.this.spanChange(buf, what, s, st, e, en);
10047 public void onSpanAdded(Spannable buf, Object what, int s, int e) {
10048 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanAdded s=" + s + " e=" + e
10049 + " what=" + what + ": " + buf);
10050 TextView.this.spanChange(buf, what, -1, s, -1, e);
10053 public void onSpanRemoved(Spannable buf, Object what, int s, int e) {
10054 if (DEBUG_EXTRACT) Log.v(LOG_TAG, "onSpanRemoved s=" + s + " e=" + e
10055 + " what=" + what + ": " + buf);
10056 TextView.this.spanChange(buf, what, s, -1, e, -1);