Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpannableString with Image example

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.

like image 607
Asahi Avatar asked Jul 04 '10 20:07

Asahi


2 Answers

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);  }  
like image 73
Asahi Avatar answered Oct 06 '22 00:10

Asahi


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

like image 34
Dedkov Vadim Avatar answered Oct 06 '22 01:10

Dedkov Vadim