63

I am looking for an example of how to build and display Android SpannableString with ImageSpan. Something like inline display of smileys.

Thanks a lot.

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
Asahi
  • 13,378
  • 12
  • 67
  • 87
  • 1
    Do you also want help for aligning string span (top, left ,right or bottom placement) with the image span in the output SpanableString? – Abhinav Saxena Dec 26 '19 at 02:53

3 Answers3

129

Found the following and it seems to do the job:

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TextView textView  = (TextView) findViewById(R.id.textview); 
    SpannableString ss = new SpannableString("abc"); 
    Drawable d = ContextCompat.getDrawable(this, R.drawable.icon32);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
    textView.setText(ss); 
} 
STG
  • 305
  • 3
  • 11
Asahi
  • 13,378
  • 12
  • 67
  • 87
20

SpannableString + ImageSpan don't work in Android API 21 & 22 (I tested in Android Studio 1.2.1.1 in emulator), but if you do this:

TextView textView  = (TextView) findViewById(R.id.textview);
textView.setTransformationMethod(null);
...
textView.setText(ss); 

SpannableString + ImageSpan will work.

I was inspired by this post: https://stackoverflow.com/a/26959656/3706042

Community
  • 1
  • 1
Dedkov Vadim
  • 426
  • 5
  • 10
  • 2
    My device that runs API `22` displays `SpannableString` + `ImageSpan` fine. I'm using `VectorDrawable`s though... – Sakiboy May 19 '17 at 07:50
  • 1
    If text has flag `textAllCaps=true` you will lose it and it wont be uppercased if you reset transformationMethod – RadekJ Apr 20 '18 at 09:19
0

If anyone is still interested, I've created the a Java method to allow adding recursively a listed drawables to a text (set at the end to a textView) based on a "string to replace".

public void appendImages(@NonNull TextView textView,
                           @NonNull String text,
                           @NonNull String toReplace,
                           Drawable... drawables){
    if(drawables != null && drawables.length > 0){
        //list of matching positions, if any
        List<Integer> positions = new ArrayList<>();
        int index = text.indexOf(toReplace);
        while (index >= 0) {
            //add position
            positions.add(index);
            index = text.indexOf(toReplace, index + toReplace.length());
        }
        if(positions.size() > 0 && drawables.length >= positions.size()){
            textView.setTransformationMethod(null);
            SpannableString ss = new SpannableString(text);
            int drawablesIndex = 0;
            for(int position : positions){
                Drawable drawable = drawables[drawablesIndex++];
                //mandatory for Drawables
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                ss.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), position, position+toReplace.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            textView.setText(ss);
        }
        else Timber.w("The amount of matches to replace is %s and the number of drawables to apply is %s", positions.size(), drawables.length);
    }
    else Timber.w("The drawables array is null or empty.");
}

Usage:

appendImages(myTextView, "This is a ^ simple ^ test", "^", drawable1, drawable2);
Akamaccio
  • 79
  • 5