I began this topic on Stack Overflow but there was no answer. Please,
help. This is an absolute nonsense!

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: <b>Hel</b> lo,wor <b>ld</b>
!!

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,wo<b>rld</b>

I also noticed that I any merging of 3 objects was wrong. I  tried to
change the order of words.

E.g. , First I was merging the second Spannable object and the third
one. The result was:
lo,wo<b>rld</b>
But when I merged this result with the first Spannable object, the
result was wrong again:
Hello,wo<b>rld</b>


!!!!!!!!!!!!!!
But the true result must be like this:

<b>Hel</b>lo,wo<b>rld</b>
!!!!!!!!!!!!!

Why it happens? I have been debugging 2 days and by the reason of
misunderstanding of an error I have made a hole in the wall by my head!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to