Example usage for android.text Spannable toString

List of usage examples for android.text Spannable toString

Introduction

In this page you can find the example usage for android.text Spannable toString.

Prototype

public String toString();

Source Link

Document

Returns a string containing the characters in this sequence in the same order as this sequence.

Usage

From source file:dev.drsoran.moloko.util.UIUtils.java

public final static void initializeTitleWithTextLayoutAsLink(View layout, String title, Spannable text,
        ClickableSpan onClickHandler) {/*w ww  .j  av  a 2 s. com*/
    initializeTitleWithTextLayout(layout, title, text.toString());

    if (text != null) {
        final TextView textView = (TextView) layout.findViewById(R.id.title_with_text_text);
        makeLink(textView, text, onClickHandler);
    }
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Overloaded to deal with spoilers./*  ww w.j a v a2 s  . c o m*/
 */
public static void setStyledTextView(Context c, TextView t, String holder, List<Spoiler> spoilers,
        FragmentManager fm) {
    if (t instanceof HtmlTextView) {
        try {
            ((HtmlTextView) t).setHtml(holder);
        } catch (Exception e) {
            logError(e.toString());
            t.setText(c.getString(R.string.bbcode_parse_error));
            t.setTypeface(t.getTypeface(), Typeface.ITALIC);
        }
    } else {
        t.setText(fromHtml(holder));
    }

    // Deal with spoilers here
    styleLinkifiedTextView(c, t); // Ensures TextView contains a spannable
    Spannable span = new SpannableString(t.getText());
    String rawSpan = span.toString();
    int startFromIndex = 0;

    for (int i = 0; i < spoilers.size(); i++) {
        Spoiler s = spoilers.get(i);
        int start = rawSpan.indexOf(s.replacer, startFromIndex);
        if (start != -1) {
            int end = start + s.replacer.length();
            startFromIndex = end;
            SpoilerSpan clickyDialog = new SpoilerSpan(c, s, fm);
            span.setSpan(clickyDialog, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    t.setText(span);
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static void highlightSpans(Spannable text, int colour) {
    // Strip all existing colour spans.
    int n = text.length();
    ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
    for (int i = oldSpans.length - 1; i >= 0; i--) {
        text.removeSpan(oldSpans[i]);/*from www . j a  v a  2  s.co m*/
    }
    // Colour the mentions and hashtags.
    String string = text.toString();
    int start;
    int end = 0;
    while (end < n) {
        char[] chars = { '#', '@' };
        FindCharsResult found = findStart(string, end, chars);
        start = found.stringIndex;
        if (start < 0) {
            break;
        }
        if (found.charIndex == 0) {
            end = findEndOfHashtag(string, start);
        } else if (found.charIndex == 1) {
            end = findEndOfMention(string, start);
        } else {
            break;
        }
        if (end < 0) {
            break;
        }
        text.setSpan(new ForegroundColorSpan(colour), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

From source file:com.tct.mail.browse.ConversationItemView.java

private void createSubject(final boolean isUnread) {
    final String badgeText = mHeader.badgeText == null ? "" : mHeader.badgeText;
    String subject = filterTag(getContext(), mHeader.conversation.subject);
    subject = Conversation.getSubjectForDisplay(mContext, badgeText, subject);

    /// TCT: add for search term highlight
    // process subject and snippet respectively @{
    SpannableStringBuilder subjectToHighlight = new SpannableStringBuilder(subject);
    boolean hasFilter = (mSearchParams != null && !TextUtils.isEmpty(mSearchParams.mFilter));
    if (hasFilter) {
        boolean fieldMatchedSubject = (mSearchParams != null
                && (SearchParams.SEARCH_FIELD_SUBJECT.equals(mSearchParams.mField)
                        || SearchParams.SEARCH_FIELD_ALL.equals(mSearchParams.mField)));
        /// TCT: Only highlight un-empty subject
        if (fieldMatchedSubject && !TextUtils.isEmpty(subject)) {
            CharSequence subjectChars = TextUtilities.highlightTermsInText(subject, mSearchParams.mFilter);
            subjectToHighlight.replace(0, subject.length(), subjectChars);
        }/* www  . j a  va  2  s.  co  m*/
    }
    /// @}
    final Spannable displayedStringBuilder = new SpannableString(subjectToHighlight);

    // since spans affect text metrics, add spans to the string before measure/layout or fancy
    // ellipsizing

    final int badgeTextLength = formatBadgeText(displayedStringBuilder, badgeText);

    if (!TextUtils.isEmpty(subject)) {
        displayedStringBuilder.setSpan(
                TextAppearanceSpan.wrap(isUnread ? sSubjectTextUnreadSpan : sSubjectTextReadSpan),
                badgeTextLength, subject.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (isActivated() && showActivatedText()) {
        displayedStringBuilder.setSpan(sActivatedTextSpan, badgeTextLength, displayedStringBuilder.length(),
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }

    final int subjectWidth = mSubjectWidth;//TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD
    final int subjectHeight = mCoordinates.subjectHeight;
    mSubjectTextView.setLayoutParams(new ViewGroup.LayoutParams(subjectWidth, subjectHeight));
    mSubjectTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mCoordinates.subjectFontSize);
    layoutViewExactly(mSubjectTextView, subjectWidth, subjectHeight);

    //[FEATURE]-Mod-BEGIN by CDTS.zhonghua.tuo,05/29/2014,FR 670064
    SpannableStringBuilder builder = new SpannableStringBuilder();
    boolean filterSubject = false;
    if (mField == UIProvider.LOCAL_SEARCH_ALL || mField == UIProvider.LOCAL_SEARCH_SUBJECT) {
        filterSubject = true;
    }
    if (mQueryText != null && filterSubject) {
        CharSequence formatSubject = displayedStringBuilder;
        formatSubject = TextUtilities.highlightTermsInText(subject, mQueryText);
        builder.append(formatSubject);
        mSubjectTextView.setText(builder);
        // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S
        //store the displayed subject for calculate the statusView's X and width
        mHeader.subjectText = builder.toString();
        // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E
    } else {
        mSubjectTextView.setText(displayedStringBuilder);
        // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S
        mHeader.subjectText = displayedStringBuilder.toString();
        // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E
    }
    //[FEATURE]-Mod-END by CDTS.zhonghua.tuo
}