RQube
RQube

Reputation: 954

spannableStringBuilder not working with multiple span in single text view

Below is a utility class to make a spannable string with required formatting

    package impressico.com.testfragmentstack;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import java.util.ArrayList;
import java.util.List;

public class SimpleSpanBuilder {
  public static final int FORMATTING_STYLE_DARK_BOLD = 1;
  public static final int FORMATTING_STYLE_DARK_BOLD_SMALL = 2;
  public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT = 3;
  public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL = 4;
  private List<SpanSection> spanSections;
  private StringBuilder stringBuilder;
  ForegroundColorSpan boldColorSpan;
  ForegroundColorSpan dimColorSpan;
  ForegroundColorSpan testColorSpan1;
  ForegroundColorSpan testColorSpan2;
  RelativeSizeSpan relativeSmallSpan;

  public SimpleSpanBuilder(Context context) {
    stringBuilder = new StringBuilder();
    spanSections = new ArrayList<>();
    boldColorSpan =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Green));
    dimColorSpan =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Blue));
    testColorSpan1 =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Black));
    testColorSpan2 =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Red));
    relativeSmallSpan = new RelativeSizeSpan(0.8f);
  }

  public SimpleSpanBuilder append(String text, int formattingStyle) {
    spanSections.add(new SpanSection(text, stringBuilder.length(), formattingStyle));
    stringBuilder.append(text);
    return this;
  }

  public SpannableStringBuilder build() {
    SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString());
    for (SpanSection section : spanSections) {
      section.apply(ssb);
    }
    return ssb;
  }

  @Override
  public String toString() {
    return stringBuilder.toString();
  }

  private class SpanSection {
    private final String text;
    private final int startIndex;
    private final int formattingStyle;

    public SpanSection(String text, int startIndex, int formattingStyle) {
      this.formattingStyle = formattingStyle;
      this.text = text;
      this.startIndex = startIndex;
    }

    public void apply(SpannableStringBuilder spanStringBuilder) {
      if (spanStringBuilder == null) return;
      switch (formattingStyle) {
        case FORMATTING_STYLE_DARK_BOLD:
          spanStringBuilder.setSpan(boldColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DARK_BOLD_SMALL:
          spanStringBuilder.setSpan(testColorSpan1, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DIM_ITALIC_LIGHT:
          spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL:
          spanStringBuilder.setSpan(testColorSpan2, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
      }
    }
  }
}

When i am trying to apply formatting on span with below calling code its not working as expected

    tv=((TextView) view.findViewById(R.id.fragment_title));
    SimpleSpanBuilder ssbTest=new SimpleSpanBuilder(getContext());
    ssbTest.append("Green",1);
    ssbTest.append("Black",2);
    ssbTest.append("Blue",3);
    ssbTest.append("Red",4);
    ssbTest.append("Green",1);
    ssbTest.append("Black",2);
    ssbTest.append("Blue",3);
    ssbTest.append("Red",4);
    tv.setText(ssbTest.build());

enter image description here

someone please help me to figure out what is wrong in this or its a bug of spannable string/textview?

Update Thanks @TdSoft for solution and @W.K.S here is the reason to extend your code

case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL: {
          CalligraphyTypefaceSpan typefaceSemiBoldItalic = new CalligraphyTypefaceSpan(typefaceSBI);
          ForegroundColorSpan dimColorSpan =
              new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_color_ffa7acb3));
          RelativeSizeSpan relativeSmallSpan = new RelativeSizeSpan(0.8f);
          spanStringBuilder.setSpan(typefaceSemiBoldItalic, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          spanStringBuilder.setSpan(relativeSmallSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        break;

I have a requirement of fixed set of formatting style, above code is being called at many places so its better to expose formatting code to callers.

Upvotes: 4

Views: 5700

Answers (2)

W.K.S
W.K.S

Reputation: 10095

I wrote the SimpleSpanBuilder class in this answer. You've extended the class and made it unnecessarily complicated. It's quite simple:

SimpleSpanBuilder ssbTest = new SimpleSpanBuilder();

ssbTest.append("Green",new ForegroundColorSpan(Color.GREEN));
ssbTest.append("Black",new ForegroundColorSpan(Color.BLACK));
ssbTest.append("Blue",new ForegroundColorSpan(Color.BLUE));
ssbTest.append("Red",new ForegroundColorSpan(Color.RED));

ssbTest.append("Green",new ForegroundColorSpan(Color.GREEN));
ssbTest.append("Black",new ForegroundColorSpan(Color.BLACK));
ssbTest.append("Blue",new ForegroundColorSpan(Color.BLUE));
ssbTest.append("Red",new ForegroundColorSpan(Color.RED));


textView.setText(ssbTest.build());

UPDATE

I haven't tested this code, but to declare a named style, you could do something like:

public static class SpanStyleSheet{

    private static ParcelableSpan[] dimItalicLightSmall;

    public static ParcelableSpan[] dimItalicLightSmall(Context context){

        if(dimItalicLightSmall == null){
            dimItalicLightSmall =  new ParcellableSpan[]{
                new CalligraphyTypefaceSpan(typefaceSBI),
                new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_color_ffa7acb3)),
                new RelativeSizeSpan(0.8f)
            }
        }
        return dimItalicLightSmall;
    }
}

SimpleSpanBuilder ssbTest = new SimpleSpanBuilder();
ssbTest.append("Green",SpanStyleSheet.dimItalicLightSmall(getContext()));
textView.setText(ssbTest.build());

Upvotes: 0

Tharindu Welagedara
Tharindu Welagedara

Reputation: 2725

I just change your Utility class a little bit please see below:

public class SimpleSpanBuilder {
    public static final int FORMATTING_STYLE_DARK_BOLD = 1;
    public static final int FORMATTING_STYLE_DARK_BOLD_SMALL = 2;
    public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT = 3;
    public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL = 4;
    private List<SpanSection> spanSections;
    private StringBuilder stringBuilder;
    RelativeSizeSpan relativeSmallSpan;
    private Context context;
    public SimpleSpanBuilder(Context context) {
        this.context = context;
        stringBuilder = new StringBuilder();
        spanSections = new ArrayList<>();
        relativeSmallSpan = new RelativeSizeSpan(0.8f);
    }

    public SimpleSpanBuilder append(String text, int formattingStyle) {
        spanSections.add(new SpanSection(text, stringBuilder.length(), formattingStyle));
        stringBuilder.append(text);
        return this;
    }

    public SpannableStringBuilder build() {
        SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString());
        for (SpanSection section : spanSections) {
            section.apply(ssb);
        }
        return ssb;
    }

    @Override
    public String toString() {
        return stringBuilder.toString();
    }

    private class SpanSection {
        private final String text;
        private final int startIndex;
        private final int formattingStyle;

        public SpanSection(String text, int startIndex, int formattingStyle) {
            this.formattingStyle = formattingStyle;
            this.text = text;
            this.startIndex = startIndex;
        }

        public void apply(SpannableStringBuilder spanStringBuilder) {
            if (spanStringBuilder == null) return;
            switch (formattingStyle) {
                case FORMATTING_STYLE_DARK_BOLD:
                    ForegroundColorSpan boldColorSpan =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Green));
                    spanStringBuilder.setSpan(boldColorSpan, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DARK_BOLD_SMALL:
                    ForegroundColorSpan testColorSpan1 =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Black));

                    spanStringBuilder.setSpan(testColorSpan1, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DIM_ITALIC_LIGHT:
                    ForegroundColorSpan dimColorSpan =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Blue));
                    spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL:
                    ForegroundColorSpan testColorSpan2 =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Red));
                    spanStringBuilder.setSpan(testColorSpan2, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
            }
        }
    }
}

Note: For relativeSmallSpan also do the same thing then it will also work fine. Try this and let us know... Gud luck

Upvotes: 4

Related Questions