Wrappable TextView with SpannableStringBuilder and ImageSpan hides the last image on each line

I am dynamically populating a multi-line TextView with elements. The string of input elements is wrapped. Each element consists of two parts: text, followed by an image. Image length - 2 characters. Elements are separated by a space. To add items, I use SpannableStringBuilder. Everything works fine, except for one. By adding a new element that moves to the next line (TextView wraps the line), the image of the previous element, which is the last in the line above, disappears, regardless of how much space is still available on that line. And if I delete the recently added item in a new line, this image will appear again. Thus, the portion of the image of each element that is the last in each row is not displayed. I am using Android 4.0.3.

Here is my code:

TextView textView = (TextView) findViewById(R.id.textview);
textView.setMovementMethod(new ScrollingMovementMethod());

SpannableStringBuilder ssb = new SpannableStringBuilder();

//then for each new element I do the following
ssb.append(" "); //space separating elements (skip for the first element)
ssb.append("text");
Bitmap image = BitmapFactory.decodeResource( getResources(), imageId );
ssb.append("  "); //prepare 2-character space for the image
ssb.setSpan( new ImageSpan(image), ssb.length() - 2, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); //put the image to the prepared space
textView.setText( ssb, BufferType.SPANNABLE );

, TextWatcher. , , . . .

+3
1

. . , . , , , . :

ssb.append(" i"); //prepare 2-characters for the image

PS: , 2 String , , Char. :

int start = ssb.length();
ssb.append('i');
ssb.setSpan(new ImageSpan(image), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+7

All Articles