Java Code Examples for android.text.SpannableStringBuilder#removeSpan()

The following examples show how to use android.text.SpannableStringBuilder#removeSpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InterfaceFragment.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public void onPreferenceAfterChange(Preference preference) {
	super.onPreferenceAfterChange(preference);
	if (preference == advancedSearchPreference && advancedSearchPreference.isChecked()) {
		SpannableStringBuilder builder = new SpannableStringBuilder
				(getText(R.string.preference_advanced_search_message));
		Object[] spans = builder.getSpans(0, builder.length(), Object.class);
		for (Object span : spans) {
			int start = builder.getSpanStart(span);
			int end = builder.getSpanEnd(span);
			int flags = builder.getSpanFlags(span);
			builder.removeSpan(span);
			builder.setSpan(new TypefaceSpan("sans-serif-medium"), start, end, flags);
		}
		MessageDialog.create(this, builder, false);
	}
}
 
Example 2
Source File: HtmlUtils.java    From materialup with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the given input using {@link TouchableUrlSpan}s
 * rather than vanilla {@link URLSpan}s so that they respond to touch.
 *
 * @param input
 * @param linkTextColor
 * @param linkHighlightColor
 * @return
 */
public static Spanned parseHtml(String input,
                                ColorStateList linkTextColor,
                                @ColorInt int linkHighlightColor) {
    SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);

    // strip any trailing newlines
    while (spanned.charAt(spanned.length() - 1) == '\n') {
        spanned = spanned.delete(spanned.length() - 1, spanned.length());
    }

    URLSpan[] urlSpans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        int start = spanned.getSpanStart(urlSpan);
        int end = spanned.getSpanEnd(urlSpan);
        spanned.removeSpan(urlSpan);
        // spanned.subSequence(start, start + 1) == "@" TODO send to our own user activity...
        // when i've written it
        spanned.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor,
                linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spanned;
}
 
Example 3
Source File: Html.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 4
Source File: CustomHtmlToSpannedConverter.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
private static void endMultiple(SpannableStringBuilder text, Class kind,
                                Object[] replArray) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        for (Object repl : replArray) {
            text.setSpan(repl, where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

}
 
Example 5
Source File: TimelineListAdapter.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Link clickable.
 *
 * @param strBuilder
 *   A string builder.
 * @param span
 *   The span with url.
 */
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            try {
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                customTabsIntent.launchUrl(context, Uri.parse(span.getURL()));
            }
            catch (Exception ignored) { }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 6
Source File: CustomHtmlToSpannedConverter.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 7
Source File: GoURLSpan.java    From droidddle with Apache License 2.0 6 votes vote down vote up
/**
 * @param spanText
 * @return true if have url
 */
public static final boolean hackURLSpanHasResult(SpannableStringBuilder spanText) {
    boolean result = false;
    URLSpan[] spans = spanText.getSpans(0, spanText.length(), URLSpan.class);
    // TODO URLSpan need change to ClickableSpan (GoURLSpan) , otherwise URLSpan can not click, not display underline.WHY?
    for (URLSpan span : spans) {
        int start = spanText.getSpanStart(span);
        int end = spanText.getSpanEnd(span);
        String url = span.getURL();
        if (url != null) {
            result = true;
            spanText.removeSpan(span);
            ClickableSpan span1 = new GoURLSpan(span.getURL());
            spanText.setSpan(span1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    return result;
}
 
Example 8
Source File: TextInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 * @param charSequence the text which will be input to TextService. Attached spans that
 * implement {@link ParcelableSpan} will also be marshaled alongside with the text.
 * @param start the beginning of the range of text (inclusive).
 * @param end the end of the range of text (exclusive).
 * @param cookie the cookie for this TextInfo
 * @param sequenceNumber the sequence number for this TextInfo
 */
public TextInfo(CharSequence charSequence, int start, int end, int cookie, int sequenceNumber) {
    if (TextUtils.isEmpty(charSequence)) {
        throw new IllegalArgumentException("charSequence is empty");
    }
    // Create a snapshot of the text including spans in case they are updated outside later.
    final SpannableStringBuilder spannableString =
            new SpannableStringBuilder(charSequence, start, end);
    // SpellCheckSpan is for internal use. We do not want to marshal this for TextService.
    final SpellCheckSpan[] spans = spannableString.getSpans(0, spannableString.length(),
            SpellCheckSpan.class);
    for (int i = 0; i < spans.length; ++i) {
        spannableString.removeSpan(spans[i]);
    }

    mCharSequence = spannableString;
    mCookie = cookie;
    mSequenceNumber = sequenceNumber;
}
 
Example 9
Source File: ServerChooserFragment.java    From Intra with Apache License 2.0 6 votes vote down vote up
private void onBuiltinServerSelected(int i) {
    description.setText(descriptions[i]);

    // Update website text with a link pointing to the correct website.
    // The string resource contains a dummy hyperlink that must be replaced with a new link to
    // the correct destination.
    CharSequence template = getResources().getText(R.string.server_choice_website_notice);
    SpannableStringBuilder websiteMessage = new SpannableStringBuilder(template);

    // Trim leading whitespace introduced by indentation in strings.xml.
    while (Character.isWhitespace(websiteMessage.charAt(0))) {
        websiteMessage.delete(0, 1);
    }

    // Replace hyperlink with a new link for the current URL.
    URLSpan templateLink =
        websiteMessage.getSpans(0, websiteMessage.length(), URLSpan.class)[0];
    int linkStart = websiteMessage.getSpanStart(templateLink);
    int linkEnd = websiteMessage.getSpanEnd(templateLink);
    websiteMessage.removeSpan(templateLink);
    websiteMessage.setSpan(new URLSpan(websiteLinks[i]), linkStart, linkEnd, 0);

    serverWebsite.setText(websiteMessage);
}
 
Example 10
Source File: AnnouncementBodyAdapter.java    From PKUCourses with GNU General Public License v3.0 6 votes vote down vote up
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(@NonNull View view) {
            String url = span.getURL();
            if (url.startsWith("http://course.pku.edu.cn")) {
                Intent intent = new Intent(mContext.getActivity(), WebViewActivity.class);
                Activity activity = mContext.getActivity();
                if (activity != null) {
                    intent.putExtra("Title", "正在打开链接...");
                    intent.putExtra("WebViewUrl", url.replaceFirst("http://course.pku.edu.cn", ""));
                }
                mContext.startActivity(intent);
            } else {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                mContext.startActivity(browserIntent);
            }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 11
Source File: Html.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private static void endFont(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Font.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Font f = (Font) obj;

        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                text.setSpan(new TextAppearanceSpan(null, 0, 0, ColorStateList.valueOf(res.getColor(colorRes)), null),
                        where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                try {
                    int c = getHtmlColor(f.mColor);
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
                            where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } catch (ParseException e) {
                    // Ignore
                }
            }
        }

        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 12
Source File: ConverterHtmlToSpanned.java    From memoir with Apache License 2.0 5 votes vote down vote up
void swapIn(SpannableStringBuilder builder) {
    int start = builder.getSpanStart(this);
    int end = builder.getSpanEnd(this);
    builder.removeSpan(this);
    if (start >= 0 && end > start && end <= builder.length()) {
        builder.setSpan(mSpan, start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    }
}
 
Example 13
Source File: AutoRunCommandListEditText.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private static String getTextWithPasswords(CharSequence seq) {
    SpannableStringBuilder lstr = new SpannableStringBuilder(seq);
    for (PasswordSpan span : lstr.getSpans(0, lstr.length(), PasswordSpan.class)) {
        lstr.replace(lstr.getSpanStart(span), lstr.getSpanEnd(span), span.mPassword);
        lstr.removeSpan(span);
    }
    return lstr.toString();
}
 
Example 14
Source File: BlockStyleListener.java    From dante with MIT License 5 votes vote down vote up
@Override
public void end(Block block, SpannableStringBuilder text) {
    final int len = text.length();
    Object obj = getLast(text, BlockStyleListener.class);
    int start = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (start < 0) {
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder
                .append("Start is < 0 in block with text: ")
                .append(text.toString())
                .append(" tags:");

        for (int i = 0; i < tags.size(); i++) {
            messageBuilder.append(" %s");
        }

        Log.e(getClass().getName(), "Block Style Listener failed, please report it with the following message \n\n: " + messageBuilder.toString());
        return;
    }

    if (start != len) {
        text.setSpan(getStyleSpan(), start, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example 15
Source File: ViewUtils.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
public static void makeLinkClickable(@NonNull SpannableStringBuilder strBuilder, @NonNull final URLSpan span, @NonNull LinkClickableSpan listener)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            listener.onClick(view, span.getURL());
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 16
Source File: HtmlParser.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
private static void end(SpannableStringBuilder text, Class<?> kind, Object repl) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example 17
Source File: CustomHtmlToSpannedConverter.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
private static void endFont(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Font.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Font f = (Font) obj;

        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                if (colorRes != 0) {
                    ColorStateList colors = res.getColorStateList(colorRes);
                    text.setSpan(new TextAppearanceSpan(null, 0, 0, colors,
                                    null), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } else {
                int c = getHtmlColor(f.mColor);
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
                            where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }

        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 18
Source File: CustomHtmlToSpannedConverter.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
private static void end(SpannableStringBuilder text, Class kind, Object repl) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

}
 
Example 19
Source File: Html.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private static void end(SpannableStringBuilder text, Class kind,
        Object repl) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example 20
Source File: FeedItemsAdapter.java    From ratebeer with GNU General Public License v3.0 4 votes vote down vote up
private CharSequence buildActivityText(Context context, FeedItem feedItem) {

		// Build a span that contains the user name and the action he/she performed
		SpannableStringBuilder builder = new SpannableStringBuilder();

		// Start with username, in bold
		builder.append(feedItem.userName);
		builder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, feedItem.userName.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		builder.append(" ");

		// For some activity types, insert the connecting string
		if (feedItem.type == FeedItem.ITEMTYPE_ISDRINKING) {
			builder.append(context.getString(R.string.feed_isdrinking));
			builder.append(" ");
			builder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), builder.length() - 1, builder.length(),
					Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		} else if (feedItem.type == FeedItem.ITEMTYPE_AWARD) {
			builder.append(context.getString(R.string.feed_wasawarded));
			builder.append(" ");
			builder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), builder.length() - 1, builder.length(),
					Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		} else if (feedItem.type == FeedItem.ITEMTYPE_PLACECHECKIN) {
			builder.append(context.getString(R.string.feed_checkedinat));
			builder.append(" ");
			builder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), builder.length() - 1, builder.length(),
					Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		}

		// Parse HTML as spannable from raw link text and replace links with a bold text
		Spanned html = Html.fromHtml(feedItem.linkText);
		builder.append(html);
		URLSpan[] links = builder.getSpans(0, builder.length(), URLSpan.class);
		for (URLSpan link : links) {
			// Make link tags bold (they are not clickable though)
			builder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), builder.getSpanStart(link), builder.getSpanEnd(link),
					builder.getSpanFlags(link));
			builder.removeSpan(link);
		}

		if (feedItem.numComments > 0) {
			builder.append(" ");
			builder.append(context.getString(R.string.feed_andmore, feedItem.numComments));
		}

		return builder;
	}