List of usage examples for android.text SpannableStringBuilder getSpans
@SuppressWarnings("unchecked") public <T> T[] getSpans(int queryStart, int queryEnd, @Nullable Class<T> kind)
From source file:Main.java
/** * Make UI TextView a html link./*from ww w .jav a 2 s . c om*/ * * @param context the context * @param textView the text view * @param html the html containing link info */ public static void makeTextViewAHTMLLink(final Context context, TextView textView, String html) { textView.setLinksClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance()); CharSequence sequence = Html.fromHtml(html); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sequence); URLSpan[] urls = spannableStringBuilder.getSpans(0, sequence.length(), URLSpan.class); for (final URLSpan urlSpan : urls) { int start = spannableStringBuilder.getSpanStart(urlSpan); int end = spannableStringBuilder.getSpanEnd(urlSpan); int flags = spannableStringBuilder.getSpanFlags(urlSpan); ClickableSpan clickable = new ClickableSpan() { public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL())); context.startActivity(intent); } @Override public void updateDrawState(TextPaint textPaint) { super.updateDrawState(textPaint); textPaint.setUnderlineText(false); } }; spannableStringBuilder.removeSpan(urlSpan); spannableStringBuilder.setSpan(clickable, start, end, flags); } textView.setText(spannableStringBuilder); }
From source file:com.nttec.everychan.ui.presentation.HtmlParser.java
/** * ? ? SpoilerSpan ForegroundColorSpan ?? ? ?? ? *///from w w w.ja v a 2s . com private static void fixSpoilerSpans(SpannableStringBuilder builder, ThemeColors themeColors) { SpoilerSpan[] spoilers = builder.getSpans(0, builder.length(), SpoilerSpan.class); for (SpoilerSpan span : spoilers) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); builder.removeSpan(span); builder.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } ClickableURLSpan[] urls = builder.getSpans(0, builder.length(), ClickableURLSpan.class); for (ClickableURLSpan span : urls) { int start = builder.getSpanStart(span); int end = builder.getSpanEnd(span); builder.setSpan(new ForegroundColorSpan(themeColors.urlLinkForeground), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
From source file:com.nttec.everychan.ui.presentation.HtmlParser.java
/** * ?? (URLSpan) ClickableURLSpan ? ? ? * @param listener ?? // w ww . j av a2 s .c o m */ private static void replaceUrls(SpannableStringBuilder builder, URLSpanClickListener listener, ThemeColors themeColors, String referer) { URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class); if (spans.length > 0) { for (URLSpan span : spans) { ClickableURLSpan.replaceURLSpan(builder, span, themeColors.urlLinkForeground) .setOnClickListener(listener).setReferer(referer); } } }
From source file:com.android.tv.dvr.ui.DvrUiHelper.java
@NonNull public static CharSequence getStyledTitleWithEpisodeNumber(Context context, String title, String seasonNumber, String episodeNumber, int episodeNumberStyleResId) { if (TextUtils.isEmpty(title)) { return ""; }/*from w w w . j a v a 2s . c om*/ SpannableStringBuilder builder; if (TextUtils.isEmpty(seasonNumber) || seasonNumber.equals("0")) { builder = TextUtils.isEmpty(episodeNumber) ? new SpannableStringBuilder(title) : new SpannableStringBuilder(Html.fromHtml(context.getString( R.string.program_title_with_episode_number_no_season, title, episodeNumber))); } else { builder = new SpannableStringBuilder(Html.fromHtml(context .getString(R.string.program_title_with_episode_number, title, seasonNumber, episodeNumber))); } Object[] spans = builder.getSpans(0, builder.length(), Object.class); if (spans.length > 0) { if (episodeNumberStyleResId != 0) { builder.setSpan(new TextAppearanceSpan(context, episodeNumberStyleResId), builder.getSpanStart(spans[0]), builder.getSpanEnd(spans[0]), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } builder.removeSpan(spans[0]); } return new SpannableString(builder); }
From source file:org.pencilsofpromise.rss.RSSFragment.java
private Spanned removeContentSpanObjects(String sb) { SpannableStringBuilder spannedStr = (SpannableStringBuilder) Html.fromHtml(sb.toString().trim()); Object[] spannedObjects = spannedStr.getSpans(0, spannedStr.length(), Object.class); for (int i = 0; i < spannedObjects.length; i++) { if (spannedObjects[i] instanceof ImageSpan) spannedStr.replace(spannedStr.getSpanStart(spannedObjects[i]), spannedStr.getSpanEnd(spannedObjects[i]), ""); }/*from w w w . ja va 2s .c om*/ return spannedStr; }
From source file:pct.droid.fragments.VideoPlayerFragment.java
@Override protected void showTimedCaptionText(final Caption text) { mDisplayHandler.post(new Runnable() { @Override/*from ww w. j av a 2 s . c o m*/ public void run() { if (text == null) { if (mSubtitleText.getText().length() > 0) { mSubtitleText.setText(""); } return; } SpannableStringBuilder styledString = (SpannableStringBuilder) Html.fromHtml(text.content); ForegroundColorSpan[] toRemoveSpans = styledString.getSpans(0, styledString.length(), ForegroundColorSpan.class); for (ForegroundColorSpan remove : toRemoveSpans) { styledString.removeSpan(remove); } if (!mSubtitleText.getText().toString().equals(styledString.toString())) { mSubtitleText.setText(styledString); } } }); }
From source file:com.googlecode.eyesfree.brailleback.NodeBrailler.java
/** * Formats {@code node} and its descendants, appending the result * to {@code sb}./*from w w w. j a v a 2 s . c om*/ */ private void formatSubtree(AccessibilityNodeInfoCompat node, Editable result) { if (!node.isVisibleToUser()) { return; } BrailleRule rule = mRuleRepository.find(node); SpannableStringBuilder subtreeResult = new SpannableStringBuilder(); rule.format(subtreeResult, mContext, node); if (rule.includeChildren(node, mContext)) { int childCount = node.getChildCount(); for (int i = 0; i < childCount; ++i) { AccessibilityNodeInfoCompat child = node.getChild(i); if (child == null) { continue; } formatSubtree(child, subtreeResult); child.recycle(); } } if (!TextUtils.isEmpty(subtreeResult)) { // If the node is accessibility focused, add the focus span // here to cover the node and its formatted children. // This is a fallback in case the formatting rule hasn't set // focus by itself. if (node.isAccessibilityFocused() && subtreeResult.getSpans(0, subtreeResult.length(), DisplaySpans.FocusSpan.class).length == 0) { DisplaySpans.addFocus(subtreeResult, 0, subtreeResult.length()); } addNodeSpanForUncovered(node, subtreeResult); StringUtils.appendWithSpaces(result, subtreeResult); } }
From source file:org.getlantern.firetweet.fragment.support.UserFragment.java
private void updateTitleColor() { final int[] location = new int[2]; mNameView.getLocationOnScreen(location); final float nameShowingRatio = (mHeaderDrawerLayout.getPaddingTop() - location[1]) / (float) mNameView.getHeight(); final int textAlpha = Math.round(0xFF * MathUtils.clamp(nameShowingRatio, 0, 1)); final FragmentActivity activity = getActivity(); final SpannableStringBuilder spannedTitle; final CharSequence title = activity.getTitle(); if (title instanceof SpannableStringBuilder) { spannedTitle = (SpannableStringBuilder) title; } else {/*from w w w .j av a2 s .c o m*/ spannedTitle = SpannableStringBuilder.valueOf(title); } final TextAlphaSpan[] spans = spannedTitle.getSpans(0, spannedTitle.length(), TextAlphaSpan.class); if (spans.length > 0) { spans[0].setAlpha(textAlpha); } else { spannedTitle.setSpan(new TextAlphaSpan(textAlpha), 0, spannedTitle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } activity.setTitle(spannedTitle); }
From source file:com.android.mail.browse.ConversationItemView.java
SpannableStringBuilder elideParticipants(List<SpannableString> parts) { final SpannableStringBuilder builder = new SpannableStringBuilder(); float totalWidth = 0; boolean ellipsize = false; float width;// w ww. j a v a 2 s . co m boolean skipToHeader = false; // start with "To: " if we're showing recipients if (mDisplayedFolder.shouldShowRecipients() && !parts.isEmpty()) { final SpannableString toHeader = SendersView.getFormattedToHeader(); CharacterStyle[] spans = toHeader.getSpans(0, toHeader.length(), CharacterStyle.class); // There is only 1 character style span; make sure we apply all the // styles to the paint object before measuring. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } totalWidth += sPaint.measureText(toHeader.toString()); builder.append(toHeader); skipToHeader = true; } final SpannableStringBuilder messageInfoString = mHeader.messageInfoString; if (!TextUtils.isEmpty(messageInfoString)) { CharacterStyle[] spans = messageInfoString.getSpans(0, messageInfoString.length(), CharacterStyle.class); // There is only 1 character style span; make sure we apply all the // styles to the paint object before measuring. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } // Paint the message info string to see if we lose space. float messageInfoWidth = sPaint.measureText(messageInfoString.toString()); totalWidth += messageInfoWidth; } SpannableString prevSender = null; SpannableString ellipsizedText; for (SpannableString sender : parts) { // There may be null sender strings if there were dupes we had to remove. if (sender == null) { continue; } // No more width available, we'll only show fixed fragments. if (ellipsize) { break; } CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class); // There is only 1 character style span. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } // If there are already senders present in this string, we need to // make sure we prepend the dividing token if (SendersView.sElidedString.equals(sender.toString())) { sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken); } else if (!skipToHeader && builder.length() > 0 && (prevSender == null || !SendersView.sElidedString.equals(prevSender.toString()))) { sender = copyStyles(spans, sSendersSplitToken + sender); } else { skipToHeader = false; } prevSender = sender; if (spans.length > 0) { spans[0].updateDrawState(sPaint); } // Measure the width of the current sender and make sure we have space width = (int) sPaint.measureText(sender.toString()); if (width + totalWidth > mSendersWidth) { // The text is too long, new line won't help. We have to // ellipsize text. ellipsize = true; width = mSendersWidth - totalWidth; // ellipsis width? ellipsizedText = copyStyles(spans, TextUtils.ellipsize(sender, sPaint, width, TruncateAt.END)); width = (int) sPaint.measureText(ellipsizedText.toString()); } else { ellipsizedText = null; } totalWidth += width; final CharSequence fragmentDisplayText; if (ellipsizedText != null) { fragmentDisplayText = ellipsizedText; } else { fragmentDisplayText = sender; } builder.append(fragmentDisplayText); } mHeader.styledMessageInfoStringOffset = builder.length(); if (!TextUtils.isEmpty(messageInfoString)) { builder.append(messageInfoString); } return builder; }
From source file:com.tct.mail.browse.ConversationItemView.java
SpannableStringBuilder elideParticipants(List<SpannableString> parts) { final SpannableStringBuilder builder = new SpannableStringBuilder(); float totalWidth = 0; boolean ellipsize = false; float width;//ww w . ja v a2 s. co m boolean skipToHeader = false; // start with "To: " if we're showing recipients if (mDisplayedFolder.shouldShowRecipients() && !parts.isEmpty()) { final SpannableString toHeader = SendersView.getFormattedToHeader(); CharacterStyle[] spans = toHeader.getSpans(0, toHeader.length(), CharacterStyle.class); // There is only 1 character style span; make sure we apply all the // styles to the paint object before measuring. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } totalWidth += sPaint.measureText(toHeader.toString()); builder.append(toHeader); skipToHeader = true; } final SpannableStringBuilder messageInfoString = mHeader.messageInfoString; if (messageInfoString.length() > 0) { CharacterStyle[] spans = messageInfoString.getSpans(0, messageInfoString.length(), CharacterStyle.class); // There is only 1 character style span; make sure we apply all the // styles to the paint object before measuring. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } // Paint the message info string to see if we lose space. float messageInfoWidth = sPaint.measureText(messageInfoString.toString()); totalWidth += messageInfoWidth; } SpannableString prevSender = null; SpannableString ellipsizedText; for (SpannableString sender : parts) { // There may be null sender strings if there were dupes we had to remove. if (sender == null) { continue; } // No more width available, we'll only show fixed fragments. if (ellipsize) { break; } CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class); // There is only 1 character style span. if (spans.length > 0) { spans[0].updateDrawState(sPaint); } // If there are already senders present in this string, we need to // make sure we prepend the dividing token if (SendersView.sElidedString.equals(sender.toString())) { prevSender = sender; sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken); } else if (!skipToHeader && builder.length() > 0 && (prevSender == null || !SendersView.sElidedString.equals(prevSender.toString()))) { prevSender = sender; sender = copyStyles(spans, sSendersSplitToken + sender); } else { prevSender = sender; skipToHeader = false; } if (spans.length > 0) { spans[0].updateDrawState(sPaint); } //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_S // Measure the width of the current sender and make sure we have space width = (int) sPaint.measureText(sender.toString()); if (width + totalWidth > mCoordinates.sendersWidth) { // The text is too long, new line won't help. We have to // ellipsize text. ellipsize = true; width = mCoordinates.sendersWidth - totalWidth; // ellipsis width? ellipsizedText = copyStyles(spans, TextUtils.ellipsize(sender, sPaint, width, TruncateAt.END)); width = (int) sPaint.measureText(ellipsizedText.toString()); } else { ellipsizedText = null; } totalWidth += width; //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_E //[FEATURE]-Add-BEGIN by CDTS.zhonghua.tuo,05/29/2014,FR 670064 CharSequence fragmentDisplayText; if (ellipsizedText != null) { fragmentDisplayText = ellipsizedText; } else { fragmentDisplayText = sender; } boolean filterSender = false; if (mField == UIProvider.LOCAL_SEARCH_ALL || mField == UIProvider.LOCAL_SEARCH_FROM) { filterSender = true; } if (mQueryText != null && filterSender) { fragmentDisplayText = TextUtilities.highlightTermsInText(fragmentDisplayText.toString(), mQueryText); } //[FEATURE]-Add-END by CDTS.zhonghua.tuo builder.append(fragmentDisplayText); } mHeader.styledMessageInfoStringOffset = builder.length(); builder.append(messageInfoString); return builder; }