SpannableStringBuilder setSpan - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for SpannableStringBuilder setSpan method.

This code example shows how to use the following methods:length, setSpan
        // Then pass a SpannedString, which is immutable, but the method should still work. 
        checkPostProcess(HtmlUtils.postprocess(getContext(), new SpannedString(builder)));
    } 
 
    private void setSpans(SpannableStringBuilder builder) {
        builder.setSpan(new ImageSpan(new ColorDrawable(), ImageSpan.ALIGN_BOTTOM), 0, 2, 0);
        builder.setSpan(new QuoteSpan(), 2, 4, 0);
        builder.setSpan(new CustomSpan(), 4, builder.length(), 0);
    } 
 
    private void checkPostProcess(Spanned ret) {
        // Newlines should be trimmed. 
        assertEquals("01234", ret.toString());
 
        // First, check the image span. 
        // - Vertical alignment should be changed to ALIGN_BASELINE 
        // - Drawable shouldn't be changed. 
        ImageSpan[] imageSpans = ret.getSpans(0, ret.length(), ImageSpan.class);
        assertEquals(1, imageSpans.length);
        assertEquals(ImageSpan.ALIGN_BASELINE, imageSpans[0].getVerticalAlignment());
8
CodeRank
This code example shows how to use the following methods:length, setSpan
        // Then pass a SpannedString, which is immutable, but the method should still work. 
        checkPostProcess(HtmlUtils.postprocess(getContext(), new SpannedString(builder)));
    } 
 
    private void setSpans(SpannableStringBuilder builder) {
        builder.setSpan(new ImageSpan(new ColorDrawable(), ImageSpan.ALIGN_BOTTOM), 0, 2, 0);
        builder.setSpan(new QuoteSpan(), 2, 4, 0);
        builder.setSpan(new CustomSpan(), 4, builder.length(), 0);
    } 
 
    private void checkPostProcess(Spanned ret) {
        // Newlines should be trimmed. 
        assertEquals("01234", ret.toString());
 
        // First, check the image span. 
        // - Vertical alignment should be changed to ALIGN_BASELINE 
        // - Drawable shouldn't be changed. 
        ImageSpan[] imageSpans = ret.getSpans(0, ret.length(), ImageSpan.class);
        assertEquals(1, imageSpans.length);
        assertEquals(ImageSpan.ALIGN_BASELINE, imageSpans[0].getVerticalAlignment());
7
CodeRank
This code example shows how to use the following methods:length, setSpan, subSequence
            mCurentBackground = newBackground;
        } 
    } 
 
    private void calculateSubjectText() { 
        if (mText == null || mText.length() == 0) {
            return; 
        } 
        boolean hasSubject = false;
        int snippetStart = 0;
        if (!TextUtils.isEmpty(mSubject)) {
            int subjectColor = getFontColor(mRead ? SUBJECT_TEXT_COLOR_READ
                    : SUBJECT_TEXT_COLOR_UNREAD);
            mText.setSpan(new ForegroundColorSpan(subjectColor), 0, mSubject.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            snippetStart = mSubject.length() + 1;
        } 
        if (!TextUtils.isEmpty(mSnippet)) {
            int snippetColor = getFontColor(mRead ? SNIPPET_TEXT_COLOR_READ
                    : SNIPPET_TEXT_COLOR_UNREAD);

7
CodeRank
This code example shows how to use the following methods:append, length, setSpan
		mListIsOrdered = isOrdered;
		mListCount = 0;
	} 
 
	private void endLi(TagMarker tm) {
		int end = mSB.length();
 
		int start = tm.getStart();
 
		if (start != end) {
			mSB.append('\n');
			Object span;
			if (mListIsOrdered) {
				span = new NumberedSpan(++mListCount);
			} else { 
				span = new BulletSpan();
			} 
			mSB.setSpan(new LeadingMarginSpan.Standard(12, 12), start, end + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			mSB.setSpan(span, start, end + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		} 

7
CodeRank
This code example shows how to use the following methods:append, setSpan
This code example shows how to use the following methods:getSpanEnd, getSpanFlags, getSpanStart, removeSpan, setSpan
    /** 
     * Replaces one span with the other. 
     */ 
    private static void replaceSpan(SpannableStringBuilder builder, Object originalSpan,
            Object newSpan) {
        builder.setSpan(newSpan,
                builder.getSpanStart(originalSpan),
                builder.getSpanEnd(originalSpan),
                builder.getSpanFlags(originalSpan));
        builder.removeSpan(originalSpan);
    } 
 
    public static class StreamItemQuoteSpan extends QuoteSpan {
        private final int mWidth;
 
        public StreamItemQuoteSpan(int color, int width) {
            super(color);
            this.mWidth = width;
        } 
 
8
CodeRank
This code example shows how to use the following methods:length, setSpan
    } 
 
    private static CharSequence formatMessage(CellBroadcastMessage message) {
        String body = message.getMessageBody();
 
        SpannableStringBuilder buf = new SpannableStringBuilder(body);
 
        // Unread messages are shown in bold 
        if (!message.isRead()) {
            buf.setSpan(STYLE_BOLD, 0, buf.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } 
        return buf;
    } 
 
    @Override 
    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
        // Speak the date first, then channel name, then message body 
        event.getText().add(mCbMessage.getSpokenDateString(getContext()));
        mChannelView.dispatchPopulateAccessibilityEvent(event);
7
CodeRank
This code example shows how to use the following methods:getSpanEnd, getSpanStart, removeSpan, setSpan
    public void setOnClickListener(IURLSpanClickListener listener) {
        this.mListener = listener;
    } 
 
    public static ClickableURLSpan replaceURLSpan(SpannableStringBuilder builder, URLSpan span, int color) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        String url = span.getURL();
 
        builder.removeSpan(span);
 
        ClickableURLSpan newSpan = new ClickableURLSpan(url);
        builder.setSpan(newSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 
        return newSpan;
    } 
} 
7
CodeRank
This code example shows how to use the following methods:append, length, setSpan
			return hint == null ? TYPE_EMPTY : TYPE_HINT;
	} 
 
	private void append(SpannableStringBuilder builder, CharSequence text,
			CharacterStyle span) {
		int start = builder.length();
		builder.append(text);
		builder.setSpan(span, start, start + text.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	} 
 
	@Override 
	public View getView(int position, View convertView, ViewGroup parent) {
		final int type = getItemViewType(position);
		final View view;
		if (convertView == null) {
			final int resource;
			if (type == TYPE_MESSAGE)
				resource = R.layout.chat_viewer_message;
			else if (type == TYPE_HINT)
7
CodeRank
This code example shows how to use the following methods:setSelection, insert, length, setSpan, toString
        }; 
        final String init = "(650) 123";
        final String expected1 = "";
        TextWatcher textWatcher = getTextWatcher();
 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        textWatcher.beforeTextChanged(number, 5, 0, 1);
        number.insert(5, "4"); // (6504) 123
        Selection.setSelection(number, 5); // make the cursor at right of 4
        textWatcher.onTextChanged(number, 5, 0, 1);
        number.setSpan(cleanupTextWatcher, 0, number.length(), 0);
        textWatcher.afterTextChanged(number);
        assertEquals(expected1, number.toString());
    } 
 
    private TextWatcher getTextWatcher() {
        return new PhoneNumberFormattingTextWatcher("US");
    } 
} 
7
CodeRank

Related SpannableStringBuilder setSpan Questions & Answers:

setSpan Method Overview
Mark the specified range of text with the specified object.
See Code Examples for other Android SpannableStringBuilder Methods: