Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in merging spannable objects

Tags:

android

I want to merge 3 spannable objects. This code works fine:

Spannable s1 = new SpannableStringBuilder("bold");
             s1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             Spannable s2 = new SpannableStringBuilder("not");
             Spannable s3 = new SpannableStringBuilder("BOLD");
             s3.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             finishSpan = (Spanned) TextUtils.concat(s1,s2);
             finishSpan = (Spanned) TextUtils.concat(finishSpan,s3);

//////////////////////////////////////////////////////

or finishSpan = (Spanned) TextUtils.concat(s1,s2,s3);

I have the same code but when I merge 3 object,the result is wrong. I have checked that the type of certain elements are true.
beginningOfModifiedSpannable is bold, selectionSpannable is normal,endOfModifiedSpannable is bold But their merging is wrong. Only the last part of the result string is bold. Why it happens?? I have the same code above and it works well!

  Spannable str = contentText.getText();
            Spannable selectionSpannable = new SpannableStringBuilder(str, selectionStart, selectionEnd);
            StyleSpan[] ss = selectionSpannable.getSpans(0, selectionSpannable.length(), StyleSpan.class);

            boolean exists = false;
            for (int i = 0; i < ss.length; i++) {
                if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
                 selectionSpannable.removeSpan(ss[i]);
                    exists = true;
                }
            }

            if (!exists){
                str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            else {

             Spannable endOfModifiedSpannable = new SpannableStringBuilder(str, selectionEnd, contentText.getText().length());
             Spannable beginningOfModifiedSpannable = new SpannableStringBuilder(str, 0, selectionStart);
             Spanned finishSpan = null;

             if(beginningOfModifiedSpannable.length() > 0) {

              if(endOfModifiedSpannable.length() > 0) {
               finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);
               finishSpan = (Spanned) TextUtils.concat(finishSpan,endOfModifiedSpannable);
                 }
              else {
               finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);
              }

             }
             else {
              if(endOfModifiedSpannable.length() > 0) {
               finishSpan = (Spanned) TextUtils.concat(selectionSpannable,endOfModifiedSpannable);
                     }
                  else {
                   finishSpan = selectionSpannable;
                  }
                 }
contentText.setText(finishSpan);

I repeat that I have checked that 3 parts are true. E.g. I have 3 parts: Hel lo,wor ld

I have noticed that the first merging is right:

finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);

It looks like Hello,wor

But the second is wrong

finishSpan = (Spanned) TextUtils.concat(finishSpan,endOfModifiedSpannable);

or this merging is wrong:

finishSpan = (Spanned) TextUtils.concat(finishSpan,selectionSpannable,endOfModifiedSpannable);

And the result of wrong string is: Hello,world

But the true result must be like this:

Hello,world

Nonsense!

like image 848
user565447 Avatar asked Apr 16 '26 21:04

user565447


1 Answers

the problem is that you most probably use the same CharacterStyle instance for both parts, the 1st and 3rd. You need to use a new instance every time you add a style to a Spannable object.

e.g.

    SpannableStringBuilder ssb = new SpannableStringBuilder("Hello,world");
    CharacterStyle c = new StyleSpan(Typeface.BOLD);
    ssb.setSpan(c, 0, 3, 0);
    ssb.setSpan(c, 9, 11, 0);

this will result into Hello,World

to get a correct result you must do something like this:

    SpannableStringBuilder ssb = new SpannableStringBuilder("Hello,world");
    CharacterStyle c = new StyleSpan(Typeface.BOLD);
    CharacterStyle c2 = new StyleSpan(Typeface.BOLD);
    ssb.setSpan(c, 0, 3, 0);
    ssb.setSpan(c2, 9, 11, 0);
like image 179
tagtraeumer Avatar answered Apr 19 '26 12:04

tagtraeumer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!