SpannableString with Image example

AndroidSpannableImagespan

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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); 
} 

Solution 2 - Android

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

Solution 3 - Android

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAsahiView Question on Stackoverflow
Solution 1 - AndroidAsahiView Answer on Stackoverflow
Solution 2 - AndroidDedkov VadimView Answer on Stackoverflow
Solution 3 - AndroidmatmacciView Answer on Stackoverflow