Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetSpan multiple times

Tags:

android

I'm trying to set two spans on a spannable string, but the only one showing up is the latter one. i was expecting the first one to show up due to index problems, but instead the second one did. Does the second set span override the first one? I saw samples that they claim works, not sure why mine doesn't. Here's the snippet. Thanks!

String targetStr = "target1";
String targetStr2 = "target2";
String str = "Lorem ipsum target1 dolor target2 sit amet";
Drawable icon = getResources().getDrawable(R.drawable.img);
icon.setBounds(0, 0, textView.getLineHeight(), textView.getLineHeight());
ImageSpan is = new ImageSpan(icon);
SpannableString text = new SpannableString(str);
int indOf = str.indexOf(targetStr);
int lenOf = targetStr.length();
text.setSpan(is, indOf, indOf + lenOf, 0);
int indOf1 = str.indexOf(targetStr2);
int lenOf1 = targetStr2.length();
text.setSpan(is, indOf1, indOf1 + lenOf1, 0);
textView.setText(text);

Answer: LOL. So stupid. The answer was that you can only use an ImageSpan once, so i had to set another image span, even if it has the same value.

like image 755
Lendl Leyba Avatar asked Oct 14 '25 09:10

Lendl Leyba


1 Answers

LOL. So stupid. The answer was that you can only use an ImageSpan once, so i had to set another image span, even if it has the same value.

like image 78
Lendl Leyba Avatar answered Oct 16 '25 21:10

Lendl Leyba



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!