Search in sources :

Example 36 with Spannable

use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.

the class TextView method moveCursorToVisibleOffset.

/**
     * Move the cursor, if needed, so that it is at an offset that is visible
     * to the user.  This will not move the cursor if it represents more than
     * one character (a selection range).  This will only work if the
     * TextView contains spannable text; otherwise it will do nothing.
     *
     * @return True if the cursor was actually moved, false otherwise.
     */
public boolean moveCursorToVisibleOffset() {
    if (!(mText instanceof Spannable)) {
        return false;
    }
    int start = getSelectionStart();
    int end = getSelectionEnd();
    if (start != end) {
        return false;
    }
    // First: make sure the line is visible on screen:
    int line = mLayout.getLineForOffset(start);
    final int top = mLayout.getLineTop(line);
    final int bottom = mLayout.getLineTop(line + 1);
    final int vspace = mBottom - mTop - getExtendedPaddingTop() - getExtendedPaddingBottom();
    int vslack = (bottom - top) / 2;
    if (vslack > vspace / 4)
        vslack = vspace / 4;
    final int vs = mScrollY;
    if (top < (vs + vslack)) {
        line = mLayout.getLineForVertical(vs + vslack + (bottom - top));
    } else if (bottom > (vspace + vs - vslack)) {
        line = mLayout.getLineForVertical(vspace + vs - vslack - (bottom - top));
    }
    // Next: make sure the character is visible on screen:
    final int hspace = mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight();
    final int hs = mScrollX;
    final int leftChar = mLayout.getOffsetForHorizontal(line, hs);
    final int rightChar = mLayout.getOffsetForHorizontal(line, hspace + hs);
    // line might contain bidirectional text
    final int lowChar = leftChar < rightChar ? leftChar : rightChar;
    final int highChar = leftChar > rightChar ? leftChar : rightChar;
    int newStart = start;
    if (newStart < lowChar) {
        newStart = lowChar;
    } else if (newStart > highChar) {
        newStart = highChar;
    }
    if (newStart != start) {
        Selection.setSelection((Spannable) mText, newStart);
        return true;
    }
    return false;
}
Also used : Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 37 with Spannable

use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.

the class TextView method setText.

private void setText(CharSequence text, BufferType type, boolean notifyBefore, int oldlen) {
    if (text == null) {
        text = "";
    }
    // If suggestions are not enabled, remove the suggestion spans from the text
    if (!isSuggestionsEnabled()) {
        text = removeSuggestionSpans(text);
    }
    if (!mUserSetTextScaleX)
        mTextPaint.setTextScaleX(1.0f);
    if (text instanceof Spanned && ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
        if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) {
            setHorizontalFadingEdgeEnabled(true);
            mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
        } else {
            setHorizontalFadingEdgeEnabled(false);
            mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
        }
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
    }
    int n = mFilters.length;
    for (int i = 0; i < n; i++) {
        CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
        if (out != null) {
            text = out;
        }
    }
    if (notifyBefore) {
        if (mText != null) {
            oldlen = mText.length();
            sendBeforeTextChanged(mText, 0, oldlen, text.length());
        } else {
            sendBeforeTextChanged("", 0, 0, text.length());
        }
    }
    boolean needEditableForNotification = false;
    if (mListeners != null && mListeners.size() != 0) {
        needEditableForNotification = true;
    }
    if (type == BufferType.EDITABLE || getKeyListener() != null || needEditableForNotification) {
        createEditorIfNeeded();
        mEditor.forgetUndoRedo();
        Editable t = mEditableFactory.newEditable(text);
        text = t;
        setFilters(t, mFilters);
        InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null)
            imm.restartInput(this);
    } else if (type == BufferType.SPANNABLE || mMovement != null) {
        text = mSpannableFactory.newSpannable(text);
    } else if (!(text instanceof CharWrapper)) {
        text = TextUtils.stringOrSpannedString(text);
    }
    if (mAutoLinkMask != 0) {
        Spannable s2;
        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }
        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;
            /*
                 * We must go ahead and set the text before changing the
                 * movement method, because setMovementMethod() may call
                 * setText() again to try to upgrade the buffer type.
                 */
            mText = text;
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }
    mBufferType = type;
    mText = text;
    if (mTransformation == null) {
        mTransformed = text;
    } else {
        mTransformed = mTransformation.getTransformation(text, this);
    }
    final int textLength = text.length();
    if (text instanceof Spannable && !mAllowTransformationLengthChange) {
        Spannable sp = (Spannable) text;
        // Remove any ChangeWatchers that might have come from other TextViews.
        final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
        final int count = watchers.length;
        for (int i = 0; i < count; i++) {
            sp.removeSpan(watchers[i]);
        }
        if (mChangeWatcher == null)
            mChangeWatcher = new ChangeWatcher();
        sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));
        if (mEditor != null)
            mEditor.addSpanWatchers(sp);
        if (mTransformation != null) {
            sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        if (mMovement != null) {
            mMovement.initialize(this, (Spannable) text);
            /*
                 * Initializing the movement method will have set the
                 * selection, so reset mSelectionMoved to keep that from
                 * interfering with the normal on-focus selection-setting.
                 */
            if (mEditor != null)
                mEditor.mSelectionMoved = false;
        }
    }
    if (mLayout != null) {
        checkForRelayout();
    }
    sendOnTextChanged(text, 0, oldlen, textLength);
    onTextChanged(text, 0, oldlen, textLength);
    notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
    if (needEditableForNotification) {
        sendAfterTextChanged((Editable) text);
    }
    // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
    if (mEditor != null)
        mEditor.prepareCursorControllers();
}
Also used : Editable(android.text.Editable) InputMethodManager(android.view.inputmethod.InputMethodManager) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 38 with Spannable

use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.

the class PhoneNumberUtils method createTtsSpannable.

/**
     * Wrap the supplied {@code CharSequence} with a {@code TtsSpan}, annotating it as
     * containing a phone number in its entirety.
     *
     * @param phoneNumber A {@code CharSequence} the entirety of which represents a phone number.
     * @return A {@code CharSequence} with appropriate annotations.
     */
public static CharSequence createTtsSpannable(CharSequence phoneNumber) {
    if (phoneNumber == null) {
        return null;
    }
    Spannable spannable = Spannable.Factory.getInstance().newSpannable(phoneNumber);
    PhoneNumberUtils.addTtsSpan(spannable, 0, spannable.length());
    return spannable;
}
Also used : Spannable(android.text.Spannable)

Example 39 with Spannable

use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.

the class LinkSpec method addLinks.

/**
     *  Scans the text of the provided TextView and turns all occurrences of
     *  the link types indicated in the mask into clickable links.  If matches
     *  are found the movement method for the TextView is set to
     *  LinkMovementMethod.
     *
     *  @param text TextView whose text is to be marked-up with links
     *  @param mask Mask to define which kinds of links will be searched.
     *
     *  @return True if at least one link is found and applied.
     */
public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) {
    if (mask == 0) {
        return false;
    }
    CharSequence t = text.getText();
    if (t instanceof Spannable) {
        if (addLinks((Spannable) t, mask)) {
            addLinkMovementMethod(text);
            return true;
        }
        return false;
    } else {
        SpannableString s = SpannableString.valueOf(t);
        if (addLinks(s, mask)) {
            addLinkMovementMethod(text);
            text.setText(s);
            return true;
        }
        return false;
    }
}
Also used : SpannableString(android.text.SpannableString) Spannable(android.text.Spannable)

Example 40 with Spannable

use of android.text.Spannable in project android_frameworks_base by DirtyUnicorns.

the class SpannableTest method testGetSpans.

@MediumTest
public void testGetSpans() {
    Spannable spannable = newSpannableWithText("abcdef");
    Object emptySpan = new Object();
    spannable.setSpan(emptySpan, 1, 1, 0);
    Object unemptySpan = new Object();
    spannable.setSpan(unemptySpan, 1, 2, 0);
    Object[] spans;
    // Empty spans are included when they merely abut the query region
    // but other spans are not, unless the query region is empty, in
    // in which case any abutting spans are returned.
    spans = spannable.getSpans(0, 1, Object.class);
    MoreAsserts.assertEquals(new Object[] { emptySpan }, spans);
    spans = spannable.getSpans(0, 2, Object.class);
    MoreAsserts.assertEquals(new Object[] { emptySpan, unemptySpan }, spans);
    spans = spannable.getSpans(1, 2, Object.class);
    MoreAsserts.assertEquals(new Object[] { emptySpan, unemptySpan }, spans);
    spans = spannable.getSpans(2, 2, Object.class);
    MoreAsserts.assertEquals(new Object[] { unemptySpan }, spans);
}
Also used : Spannable(android.text.Spannable) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

Spannable (android.text.Spannable)478 SpannableString (android.text.SpannableString)138 Paint (android.graphics.Paint)133 TextPaint (android.text.TextPaint)118 TextView (android.widget.TextView)63 View (android.view.View)58 Editable (android.text.Editable)48 ForegroundColorSpan (android.text.style.ForegroundColorSpan)46 StyleSpan (android.text.style.StyleSpan)46 SpannableStringBuilder (android.text.SpannableStringBuilder)40 Spanned (android.text.Spanned)38 Intent (android.content.Intent)35 InputMethodManager (android.view.inputmethod.InputMethodManager)34 SuggestionSpan (android.text.style.SuggestionSpan)29 ClickableSpan (android.text.style.ClickableSpan)26 URLSpan (android.text.style.URLSpan)25 AlertDialog (android.app.AlertDialog)18 SuppressLint (android.annotation.SuppressLint)17 Date (java.util.Date)17 TextAppearanceSpan (android.text.style.TextAppearanceSpan)16