SpannableStringBuilder length - 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 length method.

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
		if (d == null) {
			mSB.setSpan(new BackgroundColorSpan(0), mSB.length(), mSB.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			return; 
		} 
 
		int len = mSB.length();
		mSB.append("\uFFFC");
 
		mSB.setSpan(new ImageSpan(d, src), len, mSB.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	} 
 
	private void endFont(TagMarker tm) {
		int end = mSB.length();
		int start = tm.getStart();
		if (start != end) {
			String color = tm.getAttributes().getValue("", "color");
			String face = tm.getAttributes().getValue("", "face");
			String size = tm.getAttributes().getValue("", "size");
			if (!TextUtils.isEmpty(color)) {
				int c = Util.getHtmlColor(color);
7
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());
8
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: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:getSelectionEnd, setSelection, delete, length, toString
    public void testRemoveLastChars() { 
        final String init = "65012345678";
        final String result1 = "(650) 123-4567";
        TextWatcher textWatcher = getTextWatcher();
        // Remove the last char. 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        int len = number.length();
        textWatcher.beforeTextChanged(number, len - 1, 1, 0);
        number.delete(len - 1, len);
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length() - 1, 1, 0);
        textWatcher.afterTextChanged(number);
        assertEquals(result1, number.toString());
        assertEquals(result1.length(), Selection.getSelectionEnd(number));
        // Remove last 5 chars 
        final String result2 = "(650) 123";
        textWatcher.beforeTextChanged(number, number.length() - 4, 4, 0);
        number.delete(number.length() - 5, number.length());
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length(), 4, 0);

7
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:getSelectionEnd, setSelection, delete, length, toString
    public void testRemoveLastChars() { 
        final String init = "65012345678";
        final String result1 = "(650) 123-4567";
        TextWatcher textWatcher = getTextWatcher();
        // Remove the last char. 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        int len = number.length();
        textWatcher.beforeTextChanged(number, len - 1, 1, 0);
        number.delete(len - 1, len);
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length() - 1, 1, 0);
        textWatcher.afterTextChanged(number);
        assertEquals(result1, number.toString());
        assertEquals(result1.length(), Selection.getSelectionEnd(number));
        // Remove last 5 chars 
        final String result2 = "(650) 123";
        textWatcher.beforeTextChanged(number, number.length() - 4, 4, 0);
        number.delete(number.length() - 5, number.length());
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length(), 4, 0);

7
CodeRank
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
This code example shows how to use the following methods:append, charAt, length
	 * @param builder 
     * @return true if a newline was added 
	 */ 
	protected boolean appendNewLine(SpannableStringBuilder builder) {
 
		int len = builder.length();
 
		if (this.spanner.isStripExtraWhiteSpace()) {
			// Should never have more than 2 \n characters in a row. 
			if (len > 2 && builder.charAt(len - 1) == '\n'
					&& builder.charAt(len - 2) == '\n') {
				return false; 
			} 
		} 
 
		builder.append("\n");
 
        return true; 
	} 
} 
6
CodeRank

Related SpannableStringBuilder length Questions & Answers:

length Method Overview
Return the number of chars in the buffer.
See Code Examples for other Android SpannableStringBuilder Methods: