Android SpannableStringBuilder - top ranked examples from Open Source projects

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

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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:append, charAt, length
		if (!ignoreStyles)
			applyStylesToTag(ltag, marker);
	} 
 
	private void handleP(TagMarker tm) {
		int len = mSB.length();
 
		if (len >= 1 && mSB.charAt(len - 1) == '\n') {
			if (len >= 2 && mSB.charAt(len - 2) == '\n') {
				return; 
			} 
 
			mSB.append("\n");
			return; 
		} 
 
		if (len != 0) {
			mSB.append("\n\n");
		} 
	} 

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getSpanEnd, getSpanStart, getSpans, length, toString
        assertEquals(text, ssb.toString());
 
        // Multiple instances of the term 
        text = "The research word should be a search result";
        ssb = (SpannableStringBuilder)TextUtilities.highlightTermsInText(text, "Search");
        spans = ssb.getSpans(0, ssb.length(), BackgroundColorSpan.class);
        assertEquals(2, spans.length);
        span = spans[0];
        assertEquals(text.indexOf("search word"), ssb.getSpanStart(span));
        assertEquals(text.indexOf(" word"), ssb.getSpanEnd(span));
        span = spans[1];
        assertEquals(text.indexOf("search result"), ssb.getSpanStart(span));
        assertEquals(text.indexOf(" result"), ssb.getSpanEnd(span));
        assertEquals(text, ssb.toString());
    } 
 
    public void testHighlightTwoTermText() { 
        String text = "This should be visible";
        // We should find this, because search terms are case insensitive 
        SpannableStringBuilder ssb =
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:append, setSpan
		mOnBackground = res.getDrawable(R.drawable.toggle_button_on_holo_dark);
	} 
 
	public void attachToView(ViewGroup targetView) {
		mLabel = (TextView) targetView.getChildAt(0);
		SpannableStringBuilder ssb = new SpannableStringBuilder("Relay");
		ssb.append(String.valueOf(mRelayNumber));
		ssb.setSpan(new SubscriptSpan(), 5, 6, 0);
		ssb.setSpan(new RelativeSizeSpan(0.7f), 5, 6, 0);
		mLabel.setText(ssb);
		mButton = (ToggleButton) targetView.getChildAt(1);
		mButton.setOnCheckedChangeListener(this);
	} 
 
	public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
		if (isChecked) {
			mButton.setBackgroundDrawable(mOnBackground);
		} else { 
			mButton.setBackgroundDrawable(mOffBackground);
		} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
             * StringReader that throws a NullPointerException when a null 
             * string is passed in. 
             */ 
            throw new NullPointerException(); 
        } 
        return new SpannableStringBuilder(source); 
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Android SpannableStringBuilder Questions & Answers:

See Code Examples for Android SpannableStringBuilder Methods: