Is there any way to insert an ImageSpan in a TextView without disrupting the text?
Asked Answered
G

4

26

It seems that in order to add an ImageSpan to a Spannable in Android, I have to actually replace some text with the Image. For example:

Spannable span = new SpannableString("Foo imageplace Bar!");
Drawable android = context.getResources().getDrawable(R.drawable.android);
android.setBounds(0, 0, 32,32);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, 4, 14, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

This will replace "imageplace" with the image. Because I'm dealing with complex multi-span text and reiteration, it's a bit of a headache to insert meaningless text at each place I want the android icon. But it looks like if start and end of span are the same, the image won't be included. Is there any way around this?

Graybeard answered 11/11, 2014 at 7:34 Comment(0)
C
23

Maybe you can add an additional space to be replaced by the ImageSpan. For example,

Spannable span = new SpannableString("Foo  imageplace Bar!");
Drawable android = context.getResources().getDrawable(R.drawable.android);
android.setBounds(0, 0, 32,32);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

And you will find the image replace the additional space without disrupting the text.

Chet answered 20/4, 2015 at 4:21 Comment(0)
S
13

You have to know the length of the text, the one after you want to add the image. For example..

Drawable image = ContextCompat.getDrawable(mContext, android.R.drawable.presence_offline);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
String myText = "myText";
int textLength = myText.length();
SpannableString sb = new SpannableString(myText + "   " + "This is another text");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, textLength, textLength + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
Seiler answered 31/3, 2017 at 17:14 Comment(1)
textLength, textLength + 1 this part crashes.Anticlerical
B
0

If you want to insert Drawable at the end of the text, Drawable is hiding the last character of the text to avoid that add another character at the end of the text and start the drawable at that character.

val myText = "Your text"    

val span: Spannable = SpannableString(myText+"-")
val android: Drawable = ContextCompat.getDrawable(this, R.drawable.yourDrawable)!!
android.setBounds(0, 0, 30, 30)
val image = ImageSpan(android, ImageSpan.ALIGN_BOTTOM)
span.setSpan(image, span.indexOf("-"), span.indexOf("-")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
Bipolar answered 3/5, 2021 at 21:39 Comment(0)
Z
0

I couldn't find a way to do this. My app also has an iOS version, and on iOS, adding an NSTextAttachment attribute automatically inserts an Object Replacement Character (Unicode FFFC) and wraps the attribute around that. So in my Android app I'm adding that character where I want to place an ImageSpan. That requires some offsets in my code to account for the extra character, but at least the app works the same way on both platforms.

Here's an Object Replacement Character you can copy and paste: "".

Zena answered 11/6 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.