Code example for SpannableStringBuilder

Methods: length, setSpan

0
        // 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());
        assertEquals(ColorDrawable.class, imageSpans[0].getDrawable().getClass());